git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 0/2] builtin/sparse-checkout: add check-rules command
@ 2023-03-08 13:49 William Sprent via GitGitGadget
  2023-03-08 13:49 ` [PATCH 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag William Sprent via GitGitGadget
                   ` (3 more replies)
  0 siblings, 4 replies; 18+ messages in thread
From: William Sprent via GitGitGadget @ 2023-03-08 13:49 UTC (permalink / raw)
  To: git; +Cc: Victoria Dye, Elijah Newren, William Sprent

Hi

Back in January I submitted a patch which suggested to teach 'ls-tree' to be
able to filter its output based on sparse checkout specifications [1]. My
main motivation for doing so was (is) to enable building more tooling on top
of sparse checkouts, which is currently hampered a bit by the fact that git
doesn't expose the pattern matching rules for the sparse checkouts.

I think the main point from that thread was that the 'ls-tree' change was
conceptually a larger change that I had initially thought it was. It was
suggested that perhaps it would be more straight-forward to initially add a
command in the vein of 'git-check-ignore' before teaching all the other
commands about sparse checkout specifics, and I think that makes sense. So I
am proposing here a new 'check-rules' sub-command to 'sparse-checkout'. This
exposes the sparse checkout pattern matching logic while still keeping the
pattern specification local to the sparse-checkout command.

Since the intention is that this new behavior would not need a work tree as
it allows the user to supply a set of rules to verify the paths against, the
change that introduces the sub-command is preceded by one that removes the
'NEEDS_WORK_TREE' flag for 'sparse-checkout' and replaces it with calls to
'setup_work_tree()' to keep current behavior.

1:
https://public-inbox.org/git/pull.1459.git.1673456518993.gitgitgadget@gmail.com/

William Sprent (2):
  builtin/sparse-checkout: remove NEED_WORK_TREE flag
  builtin/sparse-checkout: add check-rules command

 Documentation/git-sparse-checkout.txt |  23 +++-
 builtin/sparse-checkout.c             | 132 +++++++++++++++++----
 git.c                                 |   2 +-
 t/t1091-sparse-checkout-builtin.sh    | 162 +++++++++++++++++++++++++-
 4 files changed, 295 insertions(+), 24 deletions(-)


base-commit: d15644fe0226af7ffc874572d968598564a230dd
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1488%2Fwilliams-unity%2Fsparse-doodle-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1488/williams-unity/sparse-doodle-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1488
-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag
  2023-03-08 13:49 [PATCH 0/2] builtin/sparse-checkout: add check-rules command William Sprent via GitGitGadget
@ 2023-03-08 13:49 ` William Sprent via GitGitGadget
  2023-03-08 18:14   ` Junio C Hamano
  2023-03-08 13:49 ` [PATCH 2/2] builtin/sparse-checkout: add check-rules command William Sprent via GitGitGadget
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 18+ messages in thread
From: William Sprent via GitGitGadget @ 2023-03-08 13:49 UTC (permalink / raw)
  To: git; +Cc: Victoria Dye, Elijah Newren, William Sprent, William Sprent

From: William Sprent <williams@unity3d.com>

In preparation for adding a sub-command to 'sparse-checkout' that can be
run in a bare repository, remove the 'NEED_WORK_TREE' flag from its
entry in the 'commands' array of 'git.c'.

To avoid that this changes any behaviour, add calls to
'setup_work_tree()' to all of the 'sparse-checkout' sub-commands and add
tests that verify that 'sparse-checkout <cmd>' still fail with a clear
error message telling the user that the command needs a work tree.

Signed-off-by: William Sprent <williams@unity3d.com>
---
 builtin/sparse-checkout.c          |  6 ++++++
 git.c                              |  2 +-
 t/t1091-sparse-checkout-builtin.sh | 33 ++++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
index c3738154918..5fdc3d9aab5 100644
--- a/builtin/sparse-checkout.c
+++ b/builtin/sparse-checkout.c
@@ -57,6 +57,7 @@ static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
 	char *sparse_filename;
 	int res;
 
+	setup_work_tree();
 	if (!core_apply_sparse_checkout)
 		die(_("this worktree is not sparse"));
 
@@ -448,6 +449,7 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
 		OPT_END(),
 	};
 
+	setup_work_tree();
 	repo_read_index(the_repository);
 
 	init_opts.cone_mode = -1;
@@ -760,6 +762,7 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
 		OPT_END(),
 	};
 
+	setup_work_tree();
 	if (!core_apply_sparse_checkout)
 		die(_("no sparse-checkout to add to"));
 
@@ -806,6 +809,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
 		OPT_END(),
 	};
 
+	setup_work_tree();
 	repo_read_index(the_repository);
 
 	set_opts.cone_mode = -1;
@@ -855,6 +859,7 @@ static int sparse_checkout_reapply(int argc, const char **argv,
 		OPT_END(),
 	};
 
+	setup_work_tree();
 	if (!core_apply_sparse_checkout)
 		die(_("must be in a sparse-checkout to reapply sparsity patterns"));
 
@@ -898,6 +903,7 @@ static int sparse_checkout_disable(int argc, const char **argv,
 	 * forcibly return to a dense checkout regardless of initial state.
 	 */
 
+	setup_work_tree();
 	argc = parse_options(argc, argv, prefix,
 			     builtin_sparse_checkout_disable_options,
 			     builtin_sparse_checkout_disable_usage, 0);
diff --git a/git.c b/git.c
index 6171fd6769d..5adc835cf10 100644
--- a/git.c
+++ b/git.c
@@ -583,7 +583,7 @@ static struct cmd_struct commands[] = {
 	{ "show-branch", cmd_show_branch, RUN_SETUP },
 	{ "show-index", cmd_show_index, RUN_SETUP_GENTLY },
 	{ "show-ref", cmd_show_ref, RUN_SETUP },
-	{ "sparse-checkout", cmd_sparse_checkout, RUN_SETUP | NEED_WORK_TREE },
+	{ "sparse-checkout", cmd_sparse_checkout, RUN_SETUP },
 	{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
 	{ "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE },
 	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh
index 627267be153..7216267aec7 100755
--- a/t/t1091-sparse-checkout-builtin.sh
+++ b/t/t1091-sparse-checkout-builtin.sh
@@ -882,4 +882,37 @@ test_expect_success 'by default, non-cone mode will warn on individual files' '
 	grep "pass a leading slash before paths.*if you want a single file" warning
 '
 
+test_expect_success 'setup bare repo' '
+	git clone --bare "file://$(pwd)/repo" bare
+'
+test_expect_success 'list fails outside work tree' '
+	test_must_fail git -C bare sparse-checkout list 2>err &&
+	test_i18ngrep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'add fails outside work tree' '
+	test_must_fail git -C bare sparse-checkout add deeper 2>err &&
+	test_i18ngrep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'set fails outside work tree' '
+	test_must_fail git -C bare sparse-checkout set deeper 2>err &&
+	test_i18ngrep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'init fails outside work tree' '
+	test_must_fail git -C bare sparse-checkout init 2>err &&
+	test_i18ngrep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'reapply fails outside work tree' '
+	test_must_fail git -C bare sparse-checkout reapply 2>err &&
+	test_i18ngrep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'disable fails outside work tree' '
+	test_must_fail git -C bare sparse-checkout disable 2>err &&
+	test_i18ngrep "this operation must be run in a work tree" err
+'
+
 test_done
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 2/2] builtin/sparse-checkout: add check-rules command
  2023-03-08 13:49 [PATCH 0/2] builtin/sparse-checkout: add check-rules command William Sprent via GitGitGadget
  2023-03-08 13:49 ` [PATCH 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag William Sprent via GitGitGadget
@ 2023-03-08 13:49 ` William Sprent via GitGitGadget
  2023-03-19  4:26   ` Elijah Newren
  2023-03-19  4:28 ` [PATCH 0/2] " Elijah Newren
  2023-03-27  7:55 ` [PATCH v2 " William Sprent via GitGitGadget
  3 siblings, 1 reply; 18+ messages in thread
From: William Sprent via GitGitGadget @ 2023-03-08 13:49 UTC (permalink / raw)
  To: git; +Cc: Victoria Dye, Elijah Newren, William Sprent, William Sprent

From: William Sprent <williams@unity3d.com>

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' 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' and
'git check-attr' 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.

Signed-off-by: William Sprent <williams@unity3d.com>
---
 Documentation/git-sparse-checkout.txt |  23 ++++-
 builtin/sparse-checkout.c             | 126 +++++++++++++++++++++----
 t/t1091-sparse-checkout-builtin.sh    | 129 +++++++++++++++++++++++++-
 3 files changed, 255 insertions(+), 23 deletions(-)

diff --git a/Documentation/git-sparse-checkout.txt b/Documentation/git-sparse-checkout.txt
index 68392d2a56e..8fdde8f53be 100644
--- a/Documentation/git-sparse-checkout.txt
+++ b/Documentation/git-sparse-checkout.txt
@@ -9,7 +9,7 @@ git-sparse-checkout - Reduce your working tree to a subset of tracked files
 SYNOPSIS
 --------
 [verse]
-'git sparse-checkout' (init | list | set | add | reapply | disable) [<options>]
+'git sparse-checkout' (init | list | set | add | reapply | disable | check-rules) [<options>]
 
 
 DESCRIPTION
@@ -135,6 +135,27 @@ paths to pass to a subsequent 'set' or 'add' command.  However,
 the disable command, so the easy restore of calling a plain `init`
 decreased in utility.
 
+'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 C-style quoted
+strings.
++
+When called with the `--rules-file <file>` 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`.
++
+The `--rules-file` flag can be combined with the `--[no]-cone` with the same
+effect as for the `set` command with the `--stdin` flag.
++
+When called with the `-z` flag the input format and output format is \0
+terminated and not quoted.
+
+
 EXAMPLES
 --------
 `git sparse-checkout set MY/DIR1 SUB/DIR2`::
diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
index 5fdc3d9aab5..969ae14a415 100644
--- a/builtin/sparse-checkout.c
+++ b/builtin/sparse-checkout.c
@@ -20,7 +20,7 @@
 static const char *empty_base = "";
 
 static char const * const builtin_sparse_checkout_usage[] = {
-	N_("git sparse-checkout (init | list | set | add | reapply | disable) [<options>]"),
+	N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<options>]"),
 	NULL
 };
 
@@ -384,13 +384,7 @@ static int set_config(enum sparse_checkout_mode mode)
 	return 0;
 }
 
-static int update_modes(int *cone_mode, int *sparse_index)
-{
-	int mode, record_mode;
-
-	/* Determine if we need to record the mode; ensure sparse checkout on */
-	record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
-
+static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
 	/* If not specified, use previous definition of cone mode */
 	if (*cone_mode == -1 && core_apply_sparse_checkout)
 		*cone_mode = core_sparse_checkout_cone;
@@ -398,12 +392,21 @@ static int update_modes(int *cone_mode, int *sparse_index)
 	/* Set cone/non-cone mode appropriately */
 	core_apply_sparse_checkout = 1;
 	if (*cone_mode == 1 || *cone_mode == -1) {
-		mode = MODE_CONE_PATTERNS;
 		core_sparse_checkout_cone = 1;
-	} else {
-		mode = MODE_ALL_PATTERNS;
-		core_sparse_checkout_cone = 0;
+		return MODE_CONE_PATTERNS;
 	}
+	core_sparse_checkout_cone = 0;
+	return MODE_ALL_PATTERNS;
+}
+
+static int update_modes(int *cone_mode, int *sparse_index)
+{
+	int mode, record_mode;
+
+	/* Determine if we need to record the mode; ensure sparse checkout on */
+	record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
+
+	mode = update_cone_mode(cone_mode);
 	if (record_mode && set_config(mode))
 		return 1;
 
@@ -547,7 +550,7 @@ static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
 
 static void add_patterns_from_input(struct pattern_list *pl,
 				    int argc, const char **argv,
-				    int use_stdin)
+				    FILE *file)
 {
 	int i;
 	if (core_sparse_checkout_cone) {
@@ -557,9 +560,9 @@ static void add_patterns_from_input(struct pattern_list *pl,
 		hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
 		pl->use_cone_patterns = 1;
 
-		if (use_stdin) {
+		if (file) {
 			struct strbuf unquoted = STRBUF_INIT;
-			while (!strbuf_getline(&line, stdin)) {
+			while (!strbuf_getline(&line, file)) {
 				if (line.buf[0] == '"') {
 					strbuf_reset(&unquoted);
 					if (unquote_c_style(&unquoted, line.buf, NULL))
@@ -581,10 +584,10 @@ static void add_patterns_from_input(struct pattern_list *pl,
 			}
 		}
 	} else {
-		if (use_stdin) {
+		if (file) {
 			struct strbuf line = STRBUF_INIT;
 
-			while (!strbuf_getline(&line, stdin)) {
+			while (!strbuf_getline(&line, file)) {
 				size_t len;
 				char *buf = strbuf_detach(&line, &len);
 				add_pattern(buf, empty_base, 0, pl, 0);
@@ -611,7 +614,8 @@ static void add_patterns_cone_mode(int argc, const char **argv,
 	struct pattern_list existing;
 	char *sparse_filename = get_sparse_checkout_filename();
 
-	add_patterns_from_input(pl, argc, argv, use_stdin);
+	add_patterns_from_input(pl, argc, argv,
+				use_stdin ? stdin : NULL);
 
 	memset(&existing, 0, sizeof(existing));
 	existing.use_cone_patterns = core_sparse_checkout_cone;
@@ -648,7 +652,7 @@ static void add_patterns_literal(int argc, const char **argv,
 					   pl, NULL, 0))
 		die(_("unable to load existing sparse-checkout patterns"));
 	free(sparse_filename);
-	add_patterns_from_input(pl, argc, argv, use_stdin);
+	add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL);
 }
 
 static int modify_pattern_list(int argc, const char **argv, int use_stdin,
@@ -667,7 +671,8 @@ static int modify_pattern_list(int argc, const char **argv, int use_stdin,
 		break;
 
 	case REPLACE:
-		add_patterns_from_input(pl, argc, argv, use_stdin);
+		add_patterns_from_input(pl, argc, argv,
+					use_stdin ? stdin : NULL);
 		break;
 	}
 
@@ -929,6 +934,86 @@ static int sparse_checkout_disable(int argc, const char **argv,
 	return set_config(MODE_NO_PATTERNS);
 }
 
+static char const * const builtin_sparse_checkout_check_rules_usage[] = {
+	N_("git sparse-checkout check-rules [-z] [--skip-checks]"
+	   "[--[no-]cone] [--rules-file <file>]"),
+	NULL
+};
+
+static struct sparse_checkout_check_rules_opts {
+	int cone_mode;
+	int null_termination;
+	char *rules_file;
+} check_rules_opts;
+
+static int check_rules(struct pattern_list *pl, int null_terminated) {
+	struct strbuf line = STRBUF_INIT;
+	struct strbuf unquoted = STRBUF_INIT;
+	char *path;
+	int line_terminator = null_terminated ? 0 : '\n';
+	strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul
+		: strbuf_getline;
+	the_repository->index->sparse_checkout_patterns = pl;
+	while (!getline_fn(&line, stdin)) {
+		path = line.buf;
+		if (!null_terminated && line.buf[0] == '"') {
+			strbuf_reset(&unquoted);
+			if (unquote_c_style(&unquoted, line.buf, NULL))
+				die(_("unable to unquote C-style string '%s'"),
+					line.buf);
+
+			path = unquoted.buf;
+		}
+
+		if (path_in_sparse_checkout(path, the_repository->index))
+			write_name_quoted(path, stdout, line_terminator);
+	}
+
+	return 0;
+}
+
+static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix)
+{
+	static struct option builtin_sparse_checkout_check_rules_options[] = {
+		OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
+			 N_("terminate input and output files by a NUL character")),
+		OPT_BOOL(0, "cone", &check_rules_opts.cone_mode,
+			 N_("when used with --rules-file interpret patterns as cone mode patterns")),
+		OPT_FILENAME(0, "rules-file", &check_rules_opts.rules_file,
+			 N_("use patterns in <file> instead of the current ones.")),
+		OPT_END(),
+	};
+
+	FILE *fp;
+	int ret;
+	struct pattern_list pl = {0};
+	char *sparse_filename;
+	check_rules_opts.cone_mode = -1;
+
+	argc = parse_options(argc, argv, prefix,
+			     builtin_sparse_checkout_check_rules_options,
+			     builtin_sparse_checkout_check_rules_usage,
+			     PARSE_OPT_KEEP_UNKNOWN_OPT);
+
+	update_cone_mode(&check_rules_opts.cone_mode);
+	pl.use_cone_patterns = core_sparse_checkout_cone;
+	if (check_rules_opts.rules_file) {
+		fp = xfopen(check_rules_opts.rules_file, "r");
+		add_patterns_from_input(&pl, argc, argv, fp);
+		fclose(fp);
+	} else {
+		sparse_filename = get_sparse_checkout_filename();
+		if (add_patterns_from_file_to_list(sparse_filename, "", 0, &pl,
+						   NULL, 0))
+			die(_("unable to load existing sparse-checkout patterns"));
+		free(sparse_filename);
+	}
+
+	ret = check_rules(&pl, check_rules_opts.null_termination);
+	clear_pattern_list(&pl);
+	return ret;
+}
+
 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
 {
 	parse_opt_subcommand_fn *fn = NULL;
@@ -939,6 +1024,7 @@ int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
 		OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
 		OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
 		OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
+		OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
 		OPT_END(),
 	};
 
diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh
index 7216267aec7..521dc914fa7 100755
--- a/t/t1091-sparse-checkout-builtin.sh
+++ b/t/t1091-sparse-checkout-builtin.sh
@@ -555,7 +555,7 @@ test_expect_success 'cone mode: set with core.ignoreCase=true' '
 	check_files repo a folder1
 '
 
-test_expect_success 'interaction with submodules' '
+test_expect_success 'setup submodules' '
 	git clone repo super &&
 	(
 		cd super &&
@@ -566,11 +566,22 @@ test_expect_success 'interaction with submodules' '
 		git commit -m "add submodule" &&
 		git sparse-checkout init --cone &&
 		git sparse-checkout set folder1
-	) &&
+	)
+'
+
+test_expect_success 'interaction with submodules' '
 	check_files super a folder1 modules &&
 	check_files super/modules/child a deep folder1 folder2
 '
 
+test_expect_success 'check-rules interaction with submodules' '
+	git -C super ls-tree --name-only -r HEAD >all-files &&
+	git -C super sparse-checkout check-rules >check-rules-matches <all-files &&
+
+	test_i18ngrep ! "modules/" check-rules-matches &&
+	test_i18ngrep "folder1/" check-rules-matches
+'
+
 test_expect_success 'different sparse-checkouts with worktrees' '
 	git -C repo sparse-checkout set --cone deep folder1 &&
 	git -C repo worktree add --detach ../worktree &&
@@ -915,4 +926,118 @@ test_expect_success 'disable fails outside work tree' '
 	test_i18ngrep "this operation must be run in a work tree" err
 '
 
+test_expect_success 'setup clean' '
+	git -C repo clean -fdx
+'
+
+test_expect_success 'check-rules cone mode' '
+	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 &&
+
+	test_i18ngrep "deep/deeper1/deepest/a" check-rules-file &&
+	test_i18ngrep ! "deep/deeper2" check-rules-file &&
+
+	test_cmp check-rules-file ls-files &&
+	test_cmp check-rules-file check-rules-default
+'
+
+test_expect_success 'check-rules non-cone mode' '
+	cat >rules <<-\EOF &&
+	deep/deeper1/deepest/a
+	EOF
+
+	git -C bare ls-tree -r --name-only HEAD >all-files &&
+	git -C bare sparse-checkout check-rules --no-cone --rules-file ../rules\
+		>check-rules-file <all-files &&
+
+	cat rules | git -C repo sparse-checkout set --no-cone --stdin &&
+	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 &&
+
+	cat >expect <<-\EOF &&
+	deep/deeper1/deepest/a
+	EOF
+
+	test_cmp expect check-rules-file &&
+	test_cmp check-rules-file ls-files &&
+	test_cmp check-rules-file check-rules-default
+'
+
+test_expect_success 'check-rules cone mode is default' '
+	cat >rules <<-\EOF &&
+	folder1
+	EOF
+
+	cat >all-files <<-\EOF &&
+	toplevel
+	folder2/file
+	folder1/file
+	EOF
+
+	cat >expect <<-\EOF &&
+	toplevel
+	folder1/file
+	EOF
+
+	git -C bare sparse-checkout check-rules \
+		--rules-file ../rules >actual <all-files &&
+
+	test_cmp expect actual
+'
+
+test_expect_success 'check-rules quoting' '
+	cat >rules <<-EOF &&
+	"folder\" a"
+	EOF
+	cat >files <<-EOF &&
+	"folder\" a/file"
+	"folder\" b/file"
+	EOF
+	cat >expect <<-EOF &&
+	"folder\" a/file"
+	EOF
+	git sparse-checkout check-rules --cone \
+		--rules-file rules >actual <files &&
+
+	test_cmp expect actual
+'
+
+test_expect_success 'check-rules null termination' '
+	cat >rules <<-EOF &&
+	"folder\" a"
+	EOF
+
+	lf_to_nul >files <<-EOF &&
+	folder" a/a
+	folder" a/b
+	folder" b/fileQ
+	EOF
+
+	cat >expect <<-EOF &&
+	folder" a/aQfolder" a/bQ
+	EOF
+
+	git sparse-checkout check-rules --cone -z \
+		--rules-file rules >actual.nul <files &&
+	nul_to_q <actual.nul >actual &&
+	echo >>actual &&
+
+	test_cmp expect actual
+'
+
+
 test_done
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag
  2023-03-08 13:49 ` [PATCH 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag William Sprent via GitGitGadget
@ 2023-03-08 18:14   ` Junio C Hamano
  2023-03-08 19:27     ` Junio C Hamano
  0 siblings, 1 reply; 18+ messages in thread
From: Junio C Hamano @ 2023-03-08 18:14 UTC (permalink / raw)
  To: William Sprent via GitGitGadget
  Cc: git, Victoria Dye, Elijah Newren, William Sprent

"William Sprent via GitGitGadget" <gitgitgadget@gmail.com> writes:

> In preparation for adding a sub-command to 'sparse-checkout' that can be
> run in a bare repository, remove the 'NEED_WORK_TREE' flag from its
> entry in the 'commands' array of 'git.c'.

Given that "sparse-checkout" is about affecting which part of the
directory hierarchies are to be materialized in the working tree,
the idea to add a subcommand that does not require a working tree to
the command by itself smells very iffy, and the changes necessary to
protect the existing commands from the breakage caused by the
decision to drop NEED_WORK_TREE bit, i.e. sprinkling many
setup_work_tree() calls, somehow looks like they are solving a
problem that did not have to get created in the first place.

But you did not find a place the feature you wanted to add with
[2/2] would fit better, perhaps, in which case, somebody else may be
able to suggest an alternative in their reviews of that step,
hopefully.



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag
  2023-03-08 18:14   ` Junio C Hamano
@ 2023-03-08 19:27     ` Junio C Hamano
  2023-03-09 15:31       ` Elijah Newren
  0 siblings, 1 reply; 18+ messages in thread
From: Junio C Hamano @ 2023-03-08 19:27 UTC (permalink / raw)
  To: William Sprent via GitGitGadget
  Cc: git, Victoria Dye, Elijah Newren, William Sprent

Junio C Hamano <gitster@pobox.com> writes:

> But you did not find a place the feature you wanted to add with
> [2/2] would fit better, perhaps, in which case, somebody else may be
> able to suggest an alternative in their reviews of that step,
> hopefully.

... oh, or perhaps the list would reach a consensus that mixing a
subcommand that does not require a working tree is not all that bad,
which I am personally OK with, too.  I didn't at all mean to say:
"this shouldn't be a subcommand there, do it somewhere else".

Thanks.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag
  2023-03-08 19:27     ` Junio C Hamano
@ 2023-03-09 15:31       ` Elijah Newren
  2023-03-09 22:19         ` Junio C Hamano
  0 siblings, 1 reply; 18+ messages in thread
From: Elijah Newren @ 2023-03-09 15:31 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: William Sprent via GitGitGadget, git, Victoria Dye,
	William Sprent

On Wed, Mar 8, 2023 at 11:27 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> Junio C Hamano <gitster@pobox.com> writes:
>
> > But you did not find a place the feature you wanted to add with
> > [2/2] would fit better, perhaps, in which case, somebody else may be
> > able to suggest an alternative in their reviews of that step,
> > hopefully.
>
> ... oh, or perhaps the list would reach a consensus that mixing a
> subcommand that does not require a working tree is not all that bad,
> which I am personally OK with, too.  I didn't at all mean to say:
> "this shouldn't be a subcommand there, do it somewhere else".

I'm of the opinion that having sparse-checkout-related functionality
all fall under the 'sparse-checkout' command makes sense.  In the
past, all such subcommands happened to require a working tree, but
William has come up with a reasonable one that doesn't.  So, I'd be in
favor of this mixing.

I'll try to get a chance to review these patches soon, perhaps this weekend.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag
  2023-03-09 15:31       ` Elijah Newren
@ 2023-03-09 22:19         ` Junio C Hamano
  0 siblings, 0 replies; 18+ messages in thread
From: Junio C Hamano @ 2023-03-09 22:19 UTC (permalink / raw)
  To: Elijah Newren
  Cc: William Sprent via GitGitGadget, git, Victoria Dye,
	William Sprent

Elijah Newren <newren@gmail.com> writes:

> I'm of the opinion that having sparse-checkout-related functionality
> all fall under the 'sparse-checkout' command makes sense.  In the
> past, all such subcommands happened to require a working tree, but
> William has come up with a reasonable one that doesn't.  So, I'd be in
> favor of this mixing.

Yup, I am OK with that reasoning myself.

> I'll try to get a chance to review these patches soon, perhaps this weekend.

Thanks.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 2/2] builtin/sparse-checkout: add check-rules command
  2023-03-08 13:49 ` [PATCH 2/2] builtin/sparse-checkout: add check-rules command William Sprent via GitGitGadget
@ 2023-03-19  4:26   ` Elijah Newren
  2023-03-20 15:49     ` William Sprent
  0 siblings, 1 reply; 18+ messages in thread
From: Elijah Newren @ 2023-03-19  4:26 UTC (permalink / raw)
  To: William Sprent via GitGitGadget; +Cc: git, Victoria Dye, William Sprent

Hi,

On Wed, Mar 8, 2023 at 5:49 AM William Sprent via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: William Sprent <williams@unity3d.com>
>
> 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' 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' and
> 'git check-attr' 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.
>
> Signed-off-by: William Sprent <williams@unity3d.com>
> ---
>  Documentation/git-sparse-checkout.txt |  23 ++++-
>  builtin/sparse-checkout.c             | 126 +++++++++++++++++++++----
>  t/t1091-sparse-checkout-builtin.sh    | 129 +++++++++++++++++++++++++-
>  3 files changed, 255 insertions(+), 23 deletions(-)
>
> diff --git a/Documentation/git-sparse-checkout.txt b/Documentation/git-sparse-checkout.txt
> index 68392d2a56e..8fdde8f53be 100644
> --- a/Documentation/git-sparse-checkout.txt
> +++ b/Documentation/git-sparse-checkout.txt
> @@ -9,7 +9,7 @@ git-sparse-checkout - Reduce your working tree to a subset of tracked files
>  SYNOPSIS
>  --------
>  [verse]
> -'git sparse-checkout' (init | list | set | add | reapply | disable) [<options>]
> +'git sparse-checkout' (init | list | set | add | reapply | disable | check-rules) [<options>]
>
>
>  DESCRIPTION
> @@ -135,6 +135,27 @@ paths to pass to a subsequent 'set' or 'add' command.  However,
>  the disable command, so the easy restore of calling a plain `init`
>  decreased in utility.
>
> +'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 C-style quoted
> +strings.
> ++
> +When called with the `--rules-file <file>` 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`.

Could we add a "(in particular, they must be newline-delimited)" to
the end of that paragraph?

I built your series locally to test, and my first attempt was:

$ git sparse-checkout set t reftable
$ echo -e "t/helper/test-userdiff.c\nreftable/basics.c\nmerge-ort.c\nDocumentation/give-up.txt"
| git sparse-checkout check-rules
t/helper/test-userdiff.c
reftable/basics.c
merge-ort.c

...which did exactly what I expected and wanted.  So, I decided to
undo the sparse checkout and use the --rules-file option:

$ echo -e "t/helper/test-userdiff.c\nreftable/basics.c\nmerge-ort.c\nDocumentation/give-up.txt"
| git sparse-checkout check-rules --rules-file <(echo t reftable)
merge-ort.c

I was perplexed.  I thought it was a bug and was typing up explaining
why this was a problem, before I re-read this sentence of your
documentation closer and realized I had just flubbed it.  I should
have ran:
$ echo -e "t/helper/test-userdiff.c\nreftable/basics.c\nmerge-ort.c\nDocumentation/give-up.txt"
| git sparse-checkout check-rules --rules-file <(echo -e
"t\nreftable")
t/helper/test-userdiff.c
reftable/basics.c
merge-ort.c

Given that this wasn't obvious to me after reading the Documentation
(it probably should have been but just wasn't), might it be worth
calling out?

> ++
> +The `--rules-file` flag can be combined with the `--[no]-cone` with the same
> +effect as for the `set` command with the `--stdin` flag.
> ++
> +When called with the `-z` flag the input format and output format is \0
> +terminated and not quoted.

I believe this does not affect the format of the --rules-file
option...but maybe it should?  Whether it does or doesn't, do we want
to call it out when explaining -z's behavior?

> +
>  EXAMPLES
>  --------
>  `git sparse-checkout set MY/DIR1 SUB/DIR2`::
> diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
> index 5fdc3d9aab5..969ae14a415 100644
> --- a/builtin/sparse-checkout.c
> +++ b/builtin/sparse-checkout.c
> @@ -20,7 +20,7 @@
>  static const char *empty_base = "";
>
>  static char const * const builtin_sparse_checkout_usage[] = {
> -       N_("git sparse-checkout (init | list | set | add | reapply | disable) [<options>]"),
> +       N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<options>]"),
>         NULL
>  };
>
> @@ -384,13 +384,7 @@ static int set_config(enum sparse_checkout_mode mode)
>         return 0;
>  }
>
> -static int update_modes(int *cone_mode, int *sparse_index)
> -{
> -       int mode, record_mode;
> -
> -       /* Determine if we need to record the mode; ensure sparse checkout on */
> -       record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
> -
> +static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
>         /* If not specified, use previous definition of cone mode */
>         if (*cone_mode == -1 && core_apply_sparse_checkout)
>                 *cone_mode = core_sparse_checkout_cone;
> @@ -398,12 +392,21 @@ static int update_modes(int *cone_mode, int *sparse_index)
>         /* Set cone/non-cone mode appropriately */
>         core_apply_sparse_checkout = 1;
>         if (*cone_mode == 1 || *cone_mode == -1) {
> -               mode = MODE_CONE_PATTERNS;
>                 core_sparse_checkout_cone = 1;
> -       } else {
> -               mode = MODE_ALL_PATTERNS;
> -               core_sparse_checkout_cone = 0;
> +               return MODE_CONE_PATTERNS;
>         }
> +       core_sparse_checkout_cone = 0;
> +       return MODE_ALL_PATTERNS;
> +}
> +
> +static int update_modes(int *cone_mode, int *sparse_index)
> +{
> +       int mode, record_mode;
> +
> +       /* Determine if we need to record the mode; ensure sparse checkout on */
> +       record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
> +
> +       mode = update_cone_mode(cone_mode);
>         if (record_mode && set_config(mode))
>                 return 1;
>
> @@ -547,7 +550,7 @@ static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
>
>  static void add_patterns_from_input(struct pattern_list *pl,
>                                     int argc, const char **argv,
> -                                   int use_stdin)
> +                                   FILE *file)
>  {
>         int i;
>         if (core_sparse_checkout_cone) {
> @@ -557,9 +560,9 @@ static void add_patterns_from_input(struct pattern_list *pl,
>                 hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
>                 pl->use_cone_patterns = 1;
>
> -               if (use_stdin) {
> +               if (file) {
>                         struct strbuf unquoted = STRBUF_INIT;
> -                       while (!strbuf_getline(&line, stdin)) {
> +                       while (!strbuf_getline(&line, file)) {
>                                 if (line.buf[0] == '"') {
>                                         strbuf_reset(&unquoted);
>                                         if (unquote_c_style(&unquoted, line.buf, NULL))
> @@ -581,10 +584,10 @@ static void add_patterns_from_input(struct pattern_list *pl,
>                         }
>                 }
>         } else {
> -               if (use_stdin) {
> +               if (file) {
>                         struct strbuf line = STRBUF_INIT;
>
> -                       while (!strbuf_getline(&line, stdin)) {
> +                       while (!strbuf_getline(&line, file)) {
>                                 size_t len;
>                                 char *buf = strbuf_detach(&line, &len);
>                                 add_pattern(buf, empty_base, 0, pl, 0);
> @@ -611,7 +614,8 @@ static void add_patterns_cone_mode(int argc, const char **argv,
>         struct pattern_list existing;
>         char *sparse_filename = get_sparse_checkout_filename();
>
> -       add_patterns_from_input(pl, argc, argv, use_stdin);
> +       add_patterns_from_input(pl, argc, argv,
> +                               use_stdin ? stdin : NULL);
>
>         memset(&existing, 0, sizeof(existing));
>         existing.use_cone_patterns = core_sparse_checkout_cone;
> @@ -648,7 +652,7 @@ static void add_patterns_literal(int argc, const char **argv,
>                                            pl, NULL, 0))
>                 die(_("unable to load existing sparse-checkout patterns"));
>         free(sparse_filename);
> -       add_patterns_from_input(pl, argc, argv, use_stdin);
> +       add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL);
>  }
>
>  static int modify_pattern_list(int argc, const char **argv, int use_stdin,
> @@ -667,7 +671,8 @@ static int modify_pattern_list(int argc, const char **argv, int use_stdin,
>                 break;
>
>         case REPLACE:
> -               add_patterns_from_input(pl, argc, argv, use_stdin);
> +               add_patterns_from_input(pl, argc, argv,
> +                                       use_stdin ? stdin : NULL);
>                 break;
>         }
>
> @@ -929,6 +934,86 @@ static int sparse_checkout_disable(int argc, const char **argv,
>         return set_config(MODE_NO_PATTERNS);
>  }
>
> +static char const * const builtin_sparse_checkout_check_rules_usage[] = {
> +       N_("git sparse-checkout check-rules [-z] [--skip-checks]"
> +          "[--[no-]cone] [--rules-file <file>]"),
> +       NULL
> +};
> +
> +static struct sparse_checkout_check_rules_opts {
> +       int cone_mode;
> +       int null_termination;
> +       char *rules_file;
> +} check_rules_opts;
> +
> +static int check_rules(struct pattern_list *pl, int null_terminated) {
> +       struct strbuf line = STRBUF_INIT;
> +       struct strbuf unquoted = STRBUF_INIT;
> +       char *path;
> +       int line_terminator = null_terminated ? 0 : '\n';
> +       strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul
> +               : strbuf_getline;
> +       the_repository->index->sparse_checkout_patterns = pl;
> +       while (!getline_fn(&line, stdin)) {

strbuf_getline_nul() and strbuf_getline() are documented as
overwriting their strbuf, so there is no need to clear line.

> +               path = line.buf;
> +               if (!null_terminated && line.buf[0] == '"') {
> +                       strbuf_reset(&unquoted);
> +                       if (unquote_c_style(&unquoted, line.buf, NULL))

unquote_c_style() is documented as appending to its strbuf, but you
handle that via the strbuf_reset() on the line before.

> +                               die(_("unable to unquote C-style string '%s'"),
> +                                       line.buf);
> +
> +                       path = unquoted.buf;
> +               }
> +
> +               if (path_in_sparse_checkout(path, the_repository->index))
> +                       write_name_quoted(path, stdout, line_terminator);
> +       }

Do you need to call strbuf_release() for both line and unquoted here?
I think you might have a small leak.

> +
> +       return 0;
> +}
> +
> +static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix)
> +{
> +       static struct option builtin_sparse_checkout_check_rules_options[] = {
> +               OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
> +                        N_("terminate input and output files by a NUL character")),
> +               OPT_BOOL(0, "cone", &check_rules_opts.cone_mode,
> +                        N_("when used with --rules-file interpret patterns as cone mode patterns")),
> +               OPT_FILENAME(0, "rules-file", &check_rules_opts.rules_file,
> +                        N_("use patterns in <file> instead of the current ones.")),
> +               OPT_END(),
> +       };
> +
> +       FILE *fp;
> +       int ret;
> +       struct pattern_list pl = {0};
> +       char *sparse_filename;
> +       check_rules_opts.cone_mode = -1;
> +
> +       argc = parse_options(argc, argv, prefix,
> +                            builtin_sparse_checkout_check_rules_options,
> +                            builtin_sparse_checkout_check_rules_usage,
> +                            PARSE_OPT_KEEP_UNKNOWN_OPT);
> +
> +       update_cone_mode(&check_rules_opts.cone_mode);
> +       pl.use_cone_patterns = core_sparse_checkout_cone;
> +       if (check_rules_opts.rules_file) {
> +               fp = xfopen(check_rules_opts.rules_file, "r");
> +               add_patterns_from_input(&pl, argc, argv, fp);
> +               fclose(fp);
> +       } else {
> +               sparse_filename = get_sparse_checkout_filename();
> +               if (add_patterns_from_file_to_list(sparse_filename, "", 0, &pl,
> +                                                  NULL, 0))
> +                       die(_("unable to load existing sparse-checkout patterns"));
> +               free(sparse_filename);
> +       }
> +
> +       ret = check_rules(&pl, check_rules_opts.null_termination);
> +       clear_pattern_list(&pl);
> +       return ret;
> +}
> +
>  int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
>  {
>         parse_opt_subcommand_fn *fn = NULL;
> @@ -939,6 +1024,7 @@ int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
>                 OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
>                 OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
>                 OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
> +               OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
>                 OPT_END(),
>         };
>
> diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh
> index 7216267aec7..521dc914fa7 100755
> --- a/t/t1091-sparse-checkout-builtin.sh
> +++ b/t/t1091-sparse-checkout-builtin.sh
> @@ -555,7 +555,7 @@ test_expect_success 'cone mode: set with core.ignoreCase=true' '
>         check_files repo a folder1
>  '
>
> -test_expect_success 'interaction with submodules' '
> +test_expect_success 'setup submodules' '
>         git clone repo super &&
>         (
>                 cd super &&
> @@ -566,11 +566,22 @@ test_expect_success 'interaction with submodules' '
>                 git commit -m "add submodule" &&
>                 git sparse-checkout init --cone &&
>                 git sparse-checkout set folder1
> -       ) &&
> +       )
> +'
> +
> +test_expect_success 'interaction with submodules' '
>         check_files super a folder1 modules &&
>         check_files super/modules/child a deep folder1 folder2
>  '
>
> +test_expect_success 'check-rules interaction with submodules' '
> +       git -C super ls-tree --name-only -r HEAD >all-files &&
> +       git -C super sparse-checkout check-rules >check-rules-matches <all-files &&
> +
> +       test_i18ngrep ! "modules/" check-rules-matches &&
> +       test_i18ngrep "folder1/" check-rules-matches

This file already uses test_i18ngrep, so continuing to use it is okay
just for consistency.  But in general test_i18ngrep is deprecated
("Should not be used and will be removed soon." according to the docs;
soon is relative) and we prefer to just use grep.

> +'
> +
>  test_expect_success 'different sparse-checkouts with worktrees' '
>         git -C repo sparse-checkout set --cone deep folder1 &&
>         git -C repo worktree add --detach ../worktree &&
> @@ -915,4 +926,118 @@ test_expect_success 'disable fails outside work tree' '
>         test_i18ngrep "this operation must be run in a work tree" err
>  '
>
> +test_expect_success 'setup clean' '
> +       git -C repo clean -fdx
> +'
> +
> +test_expect_success 'check-rules cone mode' '
> +       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 &&
> +
> +       test_i18ngrep "deep/deeper1/deepest/a" check-rules-file &&
> +       test_i18ngrep ! "deep/deeper2" check-rules-file &&
> +
> +       test_cmp check-rules-file ls-files &&
> +       test_cmp check-rules-file check-rules-default
> +'
> +
> +test_expect_success 'check-rules non-cone mode' '
> +       cat >rules <<-\EOF &&
> +       deep/deeper1/deepest/a
> +       EOF
> +
> +       git -C bare ls-tree -r --name-only HEAD >all-files &&
> +       git -C bare sparse-checkout check-rules --no-cone --rules-file ../rules\
> +               >check-rules-file <all-files &&
> +
> +       cat rules | git -C repo sparse-checkout set --no-cone --stdin &&
> +       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 &&
> +
> +       cat >expect <<-\EOF &&
> +       deep/deeper1/deepest/a
> +       EOF
> +
> +       test_cmp expect check-rules-file &&
> +       test_cmp check-rules-file ls-files &&
> +       test_cmp check-rules-file check-rules-default
> +'
> +
> +test_expect_success 'check-rules cone mode is default' '
> +       cat >rules <<-\EOF &&
> +       folder1
> +       EOF
> +
> +       cat >all-files <<-\EOF &&
> +       toplevel
> +       folder2/file
> +       folder1/file
> +       EOF
> +
> +       cat >expect <<-\EOF &&
> +       toplevel
> +       folder1/file
> +       EOF
> +
> +       git -C bare sparse-checkout check-rules \
> +               --rules-file ../rules >actual <all-files &&
> +
> +       test_cmp expect actual

This test is checking that despite whatever sparse-checkout setting we
have, that the --rules-files option makes it ignore those rules and
use the specified one instead, and in particular that cone mode is
assumed when nothing is specified with --rules-file.  The test is
stronger because the test right before this one did a non-cone-mode
checkout.  But since this test didn't itself do a non-cone-mode
checkout, that extra strength of the test could be lost by a future
contributor inserting another test between these.  It's not a big
deal, but perhaps it'd make sense to have this test add a `git
sparse-checkout set --no-cone ...` call near the beginning?

> +'
> +
> +test_expect_success 'check-rules quoting' '
> +       cat >rules <<-EOF &&
> +       "folder\" a"
> +       EOF
> +       cat >files <<-EOF &&
> +       "folder\" a/file"
> +       "folder\" b/file"
> +       EOF
> +       cat >expect <<-EOF &&
> +       "folder\" a/file"
> +       EOF
> +       git sparse-checkout check-rules --cone \
> +               --rules-file rules >actual <files &&
> +
> +       test_cmp expect actual
> +'
> +
> +test_expect_success 'check-rules null termination' '
> +       cat >rules <<-EOF &&
> +       "folder\" a"
> +       EOF
> +
> +       lf_to_nul >files <<-EOF &&
> +       folder" a/a
> +       folder" a/b
> +       folder" b/fileQ
> +       EOF
> +
> +       cat >expect <<-EOF &&
> +       folder" a/aQfolder" a/bQ
> +       EOF
> +
> +       git sparse-checkout check-rules --cone -z \
> +               --rules-file rules >actual.nul <files &&
> +       nul_to_q <actual.nul >actual &&
> +       echo >>actual &&
> +
> +       test_cmp expect actual
> +'
> +
> +
>  test_done
> --
> gitgitgadget

Overall, nicely done.  I only found minor things to comment on, and
like the overall design, explanation, and implementation.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 0/2] builtin/sparse-checkout: add check-rules command
  2023-03-08 13:49 [PATCH 0/2] builtin/sparse-checkout: add check-rules command William Sprent via GitGitGadget
  2023-03-08 13:49 ` [PATCH 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag William Sprent via GitGitGadget
  2023-03-08 13:49 ` [PATCH 2/2] builtin/sparse-checkout: add check-rules command William Sprent via GitGitGadget
@ 2023-03-19  4:28 ` Elijah Newren
  2023-03-27  7:55 ` [PATCH v2 " William Sprent via GitGitGadget
  3 siblings, 0 replies; 18+ messages in thread
From: Elijah Newren @ 2023-03-19  4:28 UTC (permalink / raw)
  To: William Sprent via GitGitGadget; +Cc: git, Victoria Dye, William Sprent

Hi,

On Wed, Mar 8, 2023 at 5:49 AM William Sprent via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> Hi
>
> Back in January I submitted a patch which suggested to teach 'ls-tree' to be
> able to filter its output based on sparse checkout specifications [1]. My
> main motivation for doing so was (is) to enable building more tooling on top
> of sparse checkouts, which is currently hampered a bit by the fact that git
> doesn't expose the pattern matching rules for the sparse checkouts.
>
> I think the main point from that thread was that the 'ls-tree' change was
> conceptually a larger change that I had initially thought it was. It was
> suggested that perhaps it would be more straight-forward to initially add a
> command in the vein of 'git-check-ignore' before teaching all the other
> commands about sparse checkout specifics, and I think that makes sense. So I
> am proposing here a new 'check-rules' sub-command to 'sparse-checkout'. This
> exposes the sparse checkout pattern matching logic while still keeping the
> pattern specification local to the sparse-checkout command.
>
> Since the intention is that this new behavior would not need a work tree as
> it allows the user to supply a set of rules to verify the paths against, the
> change that introduces the sub-command is preceded by one that removes the
> 'NEEDS_WORK_TREE' flag for 'sparse-checkout' and replaces it with calls to
> 'setup_work_tree()' to keep current behavior.
>
> 1:
> https://public-inbox.org/git/pull.1459.git.1673456518993.gitgitgadget@gmail.com/

Sorry for the delay in reviewing.  Patch 1 looks good to me.  Patch 2
looks really good as well, but there were a few minor things that we
might be able to touch up.

And now that I've played with it and tested a little bit locally, I
really like the idea of having this functionality around.  Thanks for
sending it in.

> William Sprent (2):
>   builtin/sparse-checkout: remove NEED_WORK_TREE flag
>   builtin/sparse-checkout: add check-rules command
>
>  Documentation/git-sparse-checkout.txt |  23 +++-
>  builtin/sparse-checkout.c             | 132 +++++++++++++++++----
>  git.c                                 |   2 +-
>  t/t1091-sparse-checkout-builtin.sh    | 162 +++++++++++++++++++++++++-
>  4 files changed, 295 insertions(+), 24 deletions(-)
>
>
> base-commit: d15644fe0226af7ffc874572d968598564a230dd
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1488%2Fwilliams-unity%2Fsparse-doodle-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1488/williams-unity/sparse-doodle-v1
> Pull-Request: https://github.com/gitgitgadget/git/pull/1488
> --
> gitgitgadget

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH 2/2] builtin/sparse-checkout: add check-rules command
  2023-03-19  4:26   ` Elijah Newren
@ 2023-03-20 15:49     ` William Sprent
  0 siblings, 0 replies; 18+ messages in thread
From: William Sprent @ 2023-03-20 15:49 UTC (permalink / raw)
  To: Elijah Newren, William Sprent via GitGitGadget; +Cc: git, Victoria Dye

On 19/03/2023 05.26, Elijah Newren wrote:
> Hi,
> 
> On Wed, Mar 8, 2023 at 5:49 AM William Sprent via GitGitGadget
> <gitgitgadget@gmail.com> wrote:
>>
>> From: William Sprent <williams@unity3d.com>
>>
>> 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' 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' and
>> 'git check-attr' 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.
>>
>> Signed-off-by: William Sprent <williams@unity3d.com>
>> ---
>>   Documentation/git-sparse-checkout.txt |  23 ++++-
>>   builtin/sparse-checkout.c             | 126 +++++++++++++++++++++----
>>   t/t1091-sparse-checkout-builtin.sh    | 129 +++++++++++++++++++++++++-
>>   3 files changed, 255 insertions(+), 23 deletions(-)
>>
>> diff --git a/Documentation/git-sparse-checkout.txt b/Documentation/git-sparse-checkout.txt
>> index 68392d2a56e..8fdde8f53be 100644
>> --- a/Documentation/git-sparse-checkout.txt
>> +++ b/Documentation/git-sparse-checkout.txt
>> @@ -9,7 +9,7 @@ git-sparse-checkout - Reduce your working tree to a subset of tracked files
>>   SYNOPSIS
>>   --------
>>   [verse]
>> -'git sparse-checkout' (init | list | set | add | reapply | disable) [<options>]
>> +'git sparse-checkout' (init | list | set | add | reapply | disable | check-rules) [<options>]
>>
>>
>>   DESCRIPTION
>> @@ -135,6 +135,27 @@ paths to pass to a subsequent 'set' or 'add' command.  However,
>>   the disable command, so the easy restore of calling a plain `init`
>>   decreased in utility.
>>
>> +'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 C-style quoted
>> +strings.
>> ++
>> +When called with the `--rules-file <file>` 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`.
> 
> Could we add a "(in particular, they must be newline-delimited)" to
> the end of that paragraph?
> 
> I built your series locally to test, and my first attempt was:
> 
> $ git sparse-checkout set t reftable
> $ echo -e "t/helper/test-userdiff.c\nreftable/basics.c\nmerge-ort.c\nDocumentation/give-up.txt"
> | git sparse-checkout check-rules
> t/helper/test-userdiff.c
> reftable/basics.c
> merge-ort.c
> 
> ...which did exactly what I expected and wanted.  So, I decided to
> undo the sparse checkout and use the --rules-file option:
> 
> $ echo -e "t/helper/test-userdiff.c\nreftable/basics.c\nmerge-ort.c\nDocumentation/give-up.txt"
> | git sparse-checkout check-rules --rules-file <(echo t reftable)
> merge-ort.c
> 
> I was perplexed.  I thought it was a bug and was typing up explaining
> why this was a problem, before I re-read this sentence of your
> documentation closer and realized I had just flubbed it.  I should
> have ran:
> $ echo -e "t/helper/test-userdiff.c\nreftable/basics.c\nmerge-ort.c\nDocumentation/give-up.txt"
> | git sparse-checkout check-rules --rules-file <(echo -e
> "t\nreftable")
> t/helper/test-userdiff.c
> reftable/basics.c
> merge-ort.c
> 
> Given that this wasn't obvious to me after reading the Documentation
> (it probably should have been but just wasn't), might it be worth
> calling out?
> 

Sure. I'll add that to the end. That gives us:

"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)."

>> ++
>> +The `--rules-file` flag can be combined with the `--[no]-cone` with the same
>> +effect as for the `set` command with the `--stdin` flag.
>> ++
>> +When called with the `-z` flag the input format and output format is \0
>> +terminated and not quoted.
> 
> I believe this does not affect the format of the --rules-file
> option...but maybe it should?  Whether it does or doesn't, do we want
> to call it out when explaining -z's behavior?
>

It does not affect the --rules-file option. That's right.
I think it makes sense to make it such that you can feed the exact same rules
into 'check-rules' as you can into 'set --stdin', and 'set --stdin' does not have
a -z option. And since rules are a user facing concept, I can't think of a
situation where you would want to store them as null separated records.

If we _do_ want to have a -z for the --rules-file option, I think that it should
probably be a separate option given that it isn't unlikely that you would want
to have rules stored newline separated as they are first and foremost for users
but still would prefer to not have to deal with quoted paths when authoring
a tool or a script.

But I do think it makes sense to call it out in the documentation. Maybe something
along the lines of:

"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
>>   --------
>>   `git sparse-checkout set MY/DIR1 SUB/DIR2`::
>> diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
>> index 5fdc3d9aab5..969ae14a415 100644
>> --- a/builtin/sparse-checkout.c
>> +++ b/builtin/sparse-checkout.c
>> @@ -20,7 +20,7 @@
>>   static const char *empty_base = "";
>>
>>   static char const * const builtin_sparse_checkout_usage[] = {
>> -       N_("git sparse-checkout (init | list | set | add | reapply | disable) [<options>]"),
>> +       N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<options>]"),
>>          NULL
>>   };
>>
>> @@ -384,13 +384,7 @@ static int set_config(enum sparse_checkout_mode mode)
>>          return 0;
>>   }
>>
>> -static int update_modes(int *cone_mode, int *sparse_index)
>> -{
>> -       int mode, record_mode;
>> -
>> -       /* Determine if we need to record the mode; ensure sparse checkout on */
>> -       record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
>> -
>> +static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
>>          /* If not specified, use previous definition of cone mode */
>>          if (*cone_mode == -1 && core_apply_sparse_checkout)
>>                  *cone_mode = core_sparse_checkout_cone;
>> @@ -398,12 +392,21 @@ static int update_modes(int *cone_mode, int *sparse_index)
>>          /* Set cone/non-cone mode appropriately */
>>          core_apply_sparse_checkout = 1;
>>          if (*cone_mode == 1 || *cone_mode == -1) {
>> -               mode = MODE_CONE_PATTERNS;
>>                  core_sparse_checkout_cone = 1;
>> -       } else {
>> -               mode = MODE_ALL_PATTERNS;
>> -               core_sparse_checkout_cone = 0;
>> +               return MODE_CONE_PATTERNS;
>>          }
>> +       core_sparse_checkout_cone = 0;
>> +       return MODE_ALL_PATTERNS;
>> +}
>> +
>> +static int update_modes(int *cone_mode, int *sparse_index)
>> +{
>> +       int mode, record_mode;
>> +
>> +       /* Determine if we need to record the mode; ensure sparse checkout on */
>> +       record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
>> +
>> +       mode = update_cone_mode(cone_mode);
>>          if (record_mode && set_config(mode))
>>                  return 1;
>>
>> @@ -547,7 +550,7 @@ static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
>>
>>   static void add_patterns_from_input(struct pattern_list *pl,
>>                                      int argc, const char **argv,
>> -                                   int use_stdin)
>> +                                   FILE *file)
>>   {
>>          int i;
>>          if (core_sparse_checkout_cone) {
>> @@ -557,9 +560,9 @@ static void add_patterns_from_input(struct pattern_list *pl,
>>                  hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
>>                  pl->use_cone_patterns = 1;
>>
>> -               if (use_stdin) {
>> +               if (file) {
>>                          struct strbuf unquoted = STRBUF_INIT;
>> -                       while (!strbuf_getline(&line, stdin)) {
>> +                       while (!strbuf_getline(&line, file)) {
>>                                  if (line.buf[0] == '"') {
>>                                          strbuf_reset(&unquoted);
>>                                          if (unquote_c_style(&unquoted, line.buf, NULL))
>> @@ -581,10 +584,10 @@ static void add_patterns_from_input(struct pattern_list *pl,
>>                          }
>>                  }
>>          } else {
>> -               if (use_stdin) {
>> +               if (file) {
>>                          struct strbuf line = STRBUF_INIT;
>>
>> -                       while (!strbuf_getline(&line, stdin)) {
>> +                       while (!strbuf_getline(&line, file)) {
>>                                  size_t len;
>>                                  char *buf = strbuf_detach(&line, &len);
>>                                  add_pattern(buf, empty_base, 0, pl, 0);
>> @@ -611,7 +614,8 @@ static void add_patterns_cone_mode(int argc, const char **argv,
>>          struct pattern_list existing;
>>          char *sparse_filename = get_sparse_checkout_filename();
>>
>> -       add_patterns_from_input(pl, argc, argv, use_stdin);
>> +       add_patterns_from_input(pl, argc, argv,
>> +                               use_stdin ? stdin : NULL);
>>
>>          memset(&existing, 0, sizeof(existing));
>>          existing.use_cone_patterns = core_sparse_checkout_cone;
>> @@ -648,7 +652,7 @@ static void add_patterns_literal(int argc, const char **argv,
>>                                             pl, NULL, 0))
>>                  die(_("unable to load existing sparse-checkout patterns"));
>>          free(sparse_filename);
>> -       add_patterns_from_input(pl, argc, argv, use_stdin);
>> +       add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL);
>>   }
>>
>>   static int modify_pattern_list(int argc, const char **argv, int use_stdin,
>> @@ -667,7 +671,8 @@ static int modify_pattern_list(int argc, const char **argv, int use_stdin,
>>                  break;
>>
>>          case REPLACE:
>> -               add_patterns_from_input(pl, argc, argv, use_stdin);
>> +               add_patterns_from_input(pl, argc, argv,
>> +                                       use_stdin ? stdin : NULL);
>>                  break;
>>          }
>>
>> @@ -929,6 +934,86 @@ static int sparse_checkout_disable(int argc, const char **argv,
>>          return set_config(MODE_NO_PATTERNS);
>>   }
>>
>> +static char const * const builtin_sparse_checkout_check_rules_usage[] = {
>> +       N_("git sparse-checkout check-rules [-z] [--skip-checks]"
>> +          "[--[no-]cone] [--rules-file <file>]"),
>> +       NULL
>> +};
>> +
>> +static struct sparse_checkout_check_rules_opts {
>> +       int cone_mode;
>> +       int null_termination;
>> +       char *rules_file;
>> +} check_rules_opts;
>> +
>> +static int check_rules(struct pattern_list *pl, int null_terminated) {
>> +       struct strbuf line = STRBUF_INIT;
>> +       struct strbuf unquoted = STRBUF_INIT;
>> +       char *path;
>> +       int line_terminator = null_terminated ? 0 : '\n';
>> +       strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul
>> +               : strbuf_getline;
>> +       the_repository->index->sparse_checkout_patterns = pl;
>> +       while (!getline_fn(&line, stdin)) {
> 
> strbuf_getline_nul() and strbuf_getline() are documented as
> overwriting their strbuf, so there is no need to clear line.
> 
>> +               path = line.buf;
>> +               if (!null_terminated && line.buf[0] == '"') {
>> +                       strbuf_reset(&unquoted);
>> +                       if (unquote_c_style(&unquoted, line.buf, NULL))
> 
> unquote_c_style() is documented as appending to its strbuf, but you
> handle that via the strbuf_reset() on the line before.
> 
>> +                               die(_("unable to unquote C-style string '%s'"),
>> +                                       line.buf);
>> +
>> +                       path = unquoted.buf;
>> +               }
>> +
>> +               if (path_in_sparse_checkout(path, the_repository->index))
>> +                       write_name_quoted(path, stdout, line_terminator);
>> +       }
> 
> Do you need to call strbuf_release() for both line and unquoted here?
> I think you might have a small leak.
> 

I think you are right. I'll make sure to do that.

>> +
>> +       return 0;
>> +}
>> +
>> +static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix)
>> +{
>> +       static struct option builtin_sparse_checkout_check_rules_options[] = {
>> +               OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
>> +                        N_("terminate input and output files by a NUL character")),
>> +               OPT_BOOL(0, "cone", &check_rules_opts.cone_mode,
>> +                        N_("when used with --rules-file interpret patterns as cone mode patterns")),
>> +               OPT_FILENAME(0, "rules-file", &check_rules_opts.rules_file,
>> +                        N_("use patterns in <file> instead of the current ones.")),
>> +               OPT_END(),
>> +       };
>> +
>> +       FILE *fp;
>> +       int ret;
>> +       struct pattern_list pl = {0};
>> +       char *sparse_filename;
>> +       check_rules_opts.cone_mode = -1;
>> +
>> +       argc = parse_options(argc, argv, prefix,
>> +                            builtin_sparse_checkout_check_rules_options,
>> +                            builtin_sparse_checkout_check_rules_usage,
>> +                            PARSE_OPT_KEEP_UNKNOWN_OPT);
>> +
>> +       update_cone_mode(&check_rules_opts.cone_mode);
>> +       pl.use_cone_patterns = core_sparse_checkout_cone;
>> +       if (check_rules_opts.rules_file) {
>> +               fp = xfopen(check_rules_opts.rules_file, "r");
>> +               add_patterns_from_input(&pl, argc, argv, fp);
>> +               fclose(fp);
>> +       } else {
>> +               sparse_filename = get_sparse_checkout_filename();
>> +               if (add_patterns_from_file_to_list(sparse_filename, "", 0, &pl,
>> +                                                  NULL, 0))
>> +                       die(_("unable to load existing sparse-checkout patterns"));
>> +               free(sparse_filename);
>> +       }
>> +
>> +       ret = check_rules(&pl, check_rules_opts.null_termination);
>> +       clear_pattern_list(&pl);
>> +       return ret;
>> +}
>> +
>>   int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
>>   {
>>          parse_opt_subcommand_fn *fn = NULL;
>> @@ -939,6 +1024,7 @@ int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
>>                  OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
>>                  OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
>>                  OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
>> +               OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
>>                  OPT_END(),
>>          };
>>
>> diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh
>> index 7216267aec7..521dc914fa7 100755
>> --- a/t/t1091-sparse-checkout-builtin.sh
>> +++ b/t/t1091-sparse-checkout-builtin.sh
>> @@ -555,7 +555,7 @@ test_expect_success 'cone mode: set with core.ignoreCase=true' '
>>          check_files repo a folder1
>>   '
>>
>> -test_expect_success 'interaction with submodules' '
>> +test_expect_success 'setup submodules' '
>>          git clone repo super &&
>>          (
>>                  cd super &&
>> @@ -566,11 +566,22 @@ test_expect_success 'interaction with submodules' '
>>                  git commit -m "add submodule" &&
>>                  git sparse-checkout init --cone &&
>>                  git sparse-checkout set folder1
>> -       ) &&
>> +       )
>> +'
>> +
>> +test_expect_success 'interaction with submodules' '
>>          check_files super a folder1 modules &&
>>          check_files super/modules/child a deep folder1 folder2
>>   '
>>
>> +test_expect_success 'check-rules interaction with submodules' '
>> +       git -C super ls-tree --name-only -r HEAD >all-files &&
>> +       git -C super sparse-checkout check-rules >check-rules-matches <all-files &&
>> +
>> +       test_i18ngrep ! "modules/" check-rules-matches &&
>> +       test_i18ngrep "folder1/" check-rules-matches
> 
> This file already uses test_i18ngrep, so continuing to use it is okay
> just for consistency.  But in general test_i18ngrep is deprecated
> ("Should not be used and will be removed soon." according to the docs;
> soon is relative) and we prefer to just use grep.
> 

Ah. Thanks. I didn't know. I'll leave it for consistency.

>> +'
>> +
>>   test_expect_success 'different sparse-checkouts with worktrees' '
>>          git -C repo sparse-checkout set --cone deep folder1 &&
>>          git -C repo worktree add --detach ../worktree &&
>> @@ -915,4 +926,118 @@ test_expect_success 'disable fails outside work tree' '
>>          test_i18ngrep "this operation must be run in a work tree" err
>>   '
>>
>> +test_expect_success 'setup clean' '
>> +       git -C repo clean -fdx
>> +'
>> +
>> +test_expect_success 'check-rules cone mode' '
>> +       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 &&
>> +
>> +       test_i18ngrep "deep/deeper1/deepest/a" check-rules-file &&
>> +       test_i18ngrep ! "deep/deeper2" check-rules-file &&
>> +
>> +       test_cmp check-rules-file ls-files &&
>> +       test_cmp check-rules-file check-rules-default
>> +'
>> +
>> +test_expect_success 'check-rules non-cone mode' '
>> +       cat >rules <<-\EOF &&
>> +       deep/deeper1/deepest/a
>> +       EOF
>> +
>> +       git -C bare ls-tree -r --name-only HEAD >all-files &&
>> +       git -C bare sparse-checkout check-rules --no-cone --rules-file ../rules\
>> +               >check-rules-file <all-files &&
>> +
>> +       cat rules | git -C repo sparse-checkout set --no-cone --stdin &&
>> +       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 &&
>> +
>> +       cat >expect <<-\EOF &&
>> +       deep/deeper1/deepest/a
>> +       EOF
>> +
>> +       test_cmp expect check-rules-file &&
>> +       test_cmp check-rules-file ls-files &&
>> +       test_cmp check-rules-file check-rules-default
>> +'
>> +
>> +test_expect_success 'check-rules cone mode is default' '
>> +       cat >rules <<-\EOF &&
>> +       folder1
>> +       EOF
>> +
>> +       cat >all-files <<-\EOF &&
>> +       toplevel
>> +       folder2/file
>> +       folder1/file
>> +       EOF
>> +
>> +       cat >expect <<-\EOF &&
>> +       toplevel
>> +       folder1/file
>> +       EOF
>> +
>> +       git -C bare sparse-checkout check-rules \
>> +               --rules-file ../rules >actual <all-files &&
>> +
>> +       test_cmp expect actual
> 
> This test is checking that despite whatever sparse-checkout setting we
> have, that the --rules-files option makes it ignore those rules and
> use the specified one instead, and in particular that cone mode is
> assumed when nothing is specified with --rules-file.  The test is
> stronger because the test right before this one did a non-cone-mode
> checkout.  But since this test didn't itself do a non-cone-mode
> checkout, that extra strength of the test could be lost by a future
> contributor inserting another test between these.  It's not a big
> deal, but perhaps it'd make sense to have this test add a `git
> sparse-checkout set --no-cone ...` call near the beginning?
> 

That's a good point. I'll add that.

>> +'
>> +
>> +test_expect_success 'check-rules quoting' '
>> +       cat >rules <<-EOF &&
>> +       "folder\" a"
>> +       EOF
>> +       cat >files <<-EOF &&
>> +       "folder\" a/file"
>> +       "folder\" b/file"
>> +       EOF
>> +       cat >expect <<-EOF &&
>> +       "folder\" a/file"
>> +       EOF
>> +       git sparse-checkout check-rules --cone \
>> +               --rules-file rules >actual <files &&
>> +
>> +       test_cmp expect actual
>> +'
>> +
>> +test_expect_success 'check-rules null termination' '
>> +       cat >rules <<-EOF &&
>> +       "folder\" a"
>> +       EOF
>> +
>> +       lf_to_nul >files <<-EOF &&
>> +       folder" a/a
>> +       folder" a/b
>> +       folder" b/fileQ
>> +       EOF
>> +
>> +       cat >expect <<-EOF &&
>> +       folder" a/aQfolder" a/bQ
>> +       EOF
>> +
>> +       git sparse-checkout check-rules --cone -z \
>> +               --rules-file rules >actual.nul <files &&
>> +       nul_to_q <actual.nul >actual &&
>> +       echo >>actual &&
>> +
>> +       test_cmp expect actual
>> +'
>> +
>> +
>>   test_done
>> --
>> gitgitgadget
> 
> Overall, nicely done.  I only found minor things to comment on, and
> like the overall design, explanation, and implementation.

Thanks for taking the time to review.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v2 0/2] builtin/sparse-checkout: add check-rules command
  2023-03-08 13:49 [PATCH 0/2] builtin/sparse-checkout: add check-rules command William Sprent via GitGitGadget
                   ` (2 preceding siblings ...)
  2023-03-19  4:28 ` [PATCH 0/2] " Elijah Newren
@ 2023-03-27  7:55 ` William Sprent via GitGitGadget
  2023-03-27  7:55   ` [PATCH v2 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag William Sprent via GitGitGadget
                     ` (2 more replies)
  3 siblings, 3 replies; 18+ messages in thread
From: William Sprent via GitGitGadget @ 2023-03-27  7:55 UTC (permalink / raw)
  To: git; +Cc: Victoria Dye, Elijah Newren, William Sprent

Hi

This v2 addresses comments from Elijah's review comments.

There's one thing worth highlighting. Elijah pointed out that the the
"check-rules cone mode is default" test would be stronger if the test itself
started with a 'git sparse-checkout set --no-cone' to explicitly test that
the default interpretation of the rules passed with the '--rules-file'
option is cone mode even though the current checkout is non-cone. I
implemented this and it exposed that the option did not actually behave that
way, and that the test only verified the default behaviour of a bare
repository.

I've modified the logic of the '--rules-file' option such that it defaults
to cone mode unless combined with '--no-cone', and I've added a line to the
documentation to make this more explicit.

The alternative would be to have '--rules-file' assume non cone mode when in
a non cone mode checkout, but I think this depends a bit on "how deprecated"
non-cone mode is vs. how important it is to have the option behave
consistently with 'sparse-checkout set' (which respects the current
checkout).

Changes since v1:

 * Explicitly state in documentation that '--rules-file' expects newline
   separated rules.
 * Explicitly state in documentation that '-z' does not affect the
   '--rules-file' input.
 * Fixup typo where 'When called with the --rules-file <file> flag' was
   missing "flag".
 * Fixup behaviour in 'check-rules --rules-file', such that it defaults to
   accepting cone mode patterns when in a non cone checkout.
 * Remember to release string buffers in 'check_rules()'.
 * Explicitly state in documentation that '--rules-file' defaults to cone
   mode unless combined with '--no-cone'.
 * Better test that the default of '--rules-file' is to expect '--cone-mode'
   by running 'check-rules' in a non-cone mode checkout.

William Sprent (2):
  builtin/sparse-checkout: remove NEED_WORK_TREE flag
  builtin/sparse-checkout: add check-rules command

 Documentation/git-sparse-checkout.txt |  25 +++-
 builtin/sparse-checkout.c             | 137 ++++++++++++++++++---
 git.c                                 |   2 +-
 t/t1091-sparse-checkout-builtin.sh    | 167 +++++++++++++++++++++++++-
 4 files changed, 307 insertions(+), 24 deletions(-)


base-commit: d15644fe0226af7ffc874572d968598564a230dd
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1488%2Fwilliams-unity%2Fsparse-doodle-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1488/williams-unity/sparse-doodle-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1488

Range-diff vs v1:

 1:  4b231e9beb4 = 1:  4b231e9beb4 builtin/sparse-checkout: remove NEED_WORK_TREE flag
 2:  21c8375efff ! 2:  ef6e5b4d786 builtin/sparse-checkout: add check-rules command
     @@ Documentation/git-sparse-checkout.txt: paths to pass to a subsequent 'set' or 'a
      +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 C-style quoted
     -+strings.
     ++that pathnames that begin with a double quote (") are interpreted as C-style
     ++quoted strings.
      ++
     -+When called with the `--rules-file <file>` 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`.
     ++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).
      ++
     -+The `--rules-file` flag can be combined with the `--[no]-cone` with the same
     -+effect as for the `set` command with the `--stdin` flag.
     ++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 input format and output format is \0
     -+terminated and not quoted.
     ++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
     @@ builtin/sparse-checkout.c: static int sparse_checkout_disable(int argc, const ch
      +		if (path_in_sparse_checkout(path, the_repository->index))
      +			write_name_quoted(path, stdout, line_terminator);
      +	}
     ++	strbuf_release(&line);
     ++	strbuf_release(&unquoted);
      +
      +	return 0;
      +}
     @@ builtin/sparse-checkout.c: static int sparse_checkout_disable(int argc, const ch
      +			     builtin_sparse_checkout_check_rules_usage,
      +			     PARSE_OPT_KEEP_UNKNOWN_OPT);
      +
     ++	if (check_rules_opts.rules_file && check_rules_opts.cone_mode < 0)
     ++		check_rules_opts.cone_mode = 1;
     ++
      +	update_cone_mode(&check_rules_opts.cone_mode);
      +	pl.use_cone_patterns = core_sparse_checkout_cone;
      +	if (check_rules_opts.rules_file) {
     @@ t/t1091-sparse-checkout-builtin.sh: test_expect_success 'disable fails outside w
      +	folder1/file
      +	EOF
      +
     -+	git -C bare sparse-checkout check-rules \
     ++	git -C repo sparse-checkout set --no-cone &&
     ++	git -C repo sparse-checkout check-rules \
      +		--rules-file ../rules >actual <all-files &&
      +
     -+	test_cmp expect actual
     ++	git -C bare sparse-checkout check-rules \
     ++		--rules-file ../rules >actual-bare <all-files &&
     ++
     ++	test_cmp expect actual &&
     ++	test_cmp expect actual-bare
      +'
      +
      +test_expect_success 'check-rules quoting' '

-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v2 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag
  2023-03-27  7:55 ` [PATCH v2 " William Sprent via GitGitGadget
@ 2023-03-27  7:55   ` William Sprent via GitGitGadget
  2023-03-27 17:51     ` Junio C Hamano
  2023-03-27  7:55   ` [PATCH v2 2/2] builtin/sparse-checkout: add check-rules command William Sprent via GitGitGadget
  2023-04-01 18:49   ` [PATCH v2 0/2] " Elijah Newren
  2 siblings, 1 reply; 18+ messages in thread
From: William Sprent via GitGitGadget @ 2023-03-27  7:55 UTC (permalink / raw)
  To: git; +Cc: Victoria Dye, Elijah Newren, William Sprent, William Sprent

From: William Sprent <williams@unity3d.com>

In preparation for adding a sub-command to 'sparse-checkout' that can be
run in a bare repository, remove the 'NEED_WORK_TREE' flag from its
entry in the 'commands' array of 'git.c'.

To avoid that this changes any behaviour, add calls to
'setup_work_tree()' to all of the 'sparse-checkout' sub-commands and add
tests that verify that 'sparse-checkout <cmd>' still fail with a clear
error message telling the user that the command needs a work tree.

Signed-off-by: William Sprent <williams@unity3d.com>
---
 builtin/sparse-checkout.c          |  6 ++++++
 git.c                              |  2 +-
 t/t1091-sparse-checkout-builtin.sh | 33 ++++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
index c3738154918..5fdc3d9aab5 100644
--- a/builtin/sparse-checkout.c
+++ b/builtin/sparse-checkout.c
@@ -57,6 +57,7 @@ static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
 	char *sparse_filename;
 	int res;
 
+	setup_work_tree();
 	if (!core_apply_sparse_checkout)
 		die(_("this worktree is not sparse"));
 
@@ -448,6 +449,7 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
 		OPT_END(),
 	};
 
+	setup_work_tree();
 	repo_read_index(the_repository);
 
 	init_opts.cone_mode = -1;
@@ -760,6 +762,7 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
 		OPT_END(),
 	};
 
+	setup_work_tree();
 	if (!core_apply_sparse_checkout)
 		die(_("no sparse-checkout to add to"));
 
@@ -806,6 +809,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
 		OPT_END(),
 	};
 
+	setup_work_tree();
 	repo_read_index(the_repository);
 
 	set_opts.cone_mode = -1;
@@ -855,6 +859,7 @@ static int sparse_checkout_reapply(int argc, const char **argv,
 		OPT_END(),
 	};
 
+	setup_work_tree();
 	if (!core_apply_sparse_checkout)
 		die(_("must be in a sparse-checkout to reapply sparsity patterns"));
 
@@ -898,6 +903,7 @@ static int sparse_checkout_disable(int argc, const char **argv,
 	 * forcibly return to a dense checkout regardless of initial state.
 	 */
 
+	setup_work_tree();
 	argc = parse_options(argc, argv, prefix,
 			     builtin_sparse_checkout_disable_options,
 			     builtin_sparse_checkout_disable_usage, 0);
diff --git a/git.c b/git.c
index 6171fd6769d..5adc835cf10 100644
--- a/git.c
+++ b/git.c
@@ -583,7 +583,7 @@ static struct cmd_struct commands[] = {
 	{ "show-branch", cmd_show_branch, RUN_SETUP },
 	{ "show-index", cmd_show_index, RUN_SETUP_GENTLY },
 	{ "show-ref", cmd_show_ref, RUN_SETUP },
-	{ "sparse-checkout", cmd_sparse_checkout, RUN_SETUP | NEED_WORK_TREE },
+	{ "sparse-checkout", cmd_sparse_checkout, RUN_SETUP },
 	{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
 	{ "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE },
 	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh
index 627267be153..7216267aec7 100755
--- a/t/t1091-sparse-checkout-builtin.sh
+++ b/t/t1091-sparse-checkout-builtin.sh
@@ -882,4 +882,37 @@ test_expect_success 'by default, non-cone mode will warn on individual files' '
 	grep "pass a leading slash before paths.*if you want a single file" warning
 '
 
+test_expect_success 'setup bare repo' '
+	git clone --bare "file://$(pwd)/repo" bare
+'
+test_expect_success 'list fails outside work tree' '
+	test_must_fail git -C bare sparse-checkout list 2>err &&
+	test_i18ngrep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'add fails outside work tree' '
+	test_must_fail git -C bare sparse-checkout add deeper 2>err &&
+	test_i18ngrep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'set fails outside work tree' '
+	test_must_fail git -C bare sparse-checkout set deeper 2>err &&
+	test_i18ngrep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'init fails outside work tree' '
+	test_must_fail git -C bare sparse-checkout init 2>err &&
+	test_i18ngrep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'reapply fails outside work tree' '
+	test_must_fail git -C bare sparse-checkout reapply 2>err &&
+	test_i18ngrep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'disable fails outside work tree' '
+	test_must_fail git -C bare sparse-checkout disable 2>err &&
+	test_i18ngrep "this operation must be run in a work tree" err
+'
+
 test_done
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 2/2] builtin/sparse-checkout: add check-rules command
  2023-03-27  7:55 ` [PATCH v2 " William Sprent via GitGitGadget
  2023-03-27  7:55   ` [PATCH v2 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag William Sprent via GitGitGadget
@ 2023-03-27  7:55   ` William Sprent via GitGitGadget
  2023-04-01 18:49   ` [PATCH v2 0/2] " Elijah Newren
  2 siblings, 0 replies; 18+ messages in thread
From: William Sprent via GitGitGadget @ 2023-03-27  7:55 UTC (permalink / raw)
  To: git; +Cc: Victoria Dye, Elijah Newren, William Sprent, William Sprent

From: William Sprent <williams@unity3d.com>

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' 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' and
'git check-attr' 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.

Signed-off-by: William Sprent <williams@unity3d.com>
---
 Documentation/git-sparse-checkout.txt |  25 ++++-
 builtin/sparse-checkout.c             | 131 +++++++++++++++++++++----
 t/t1091-sparse-checkout-builtin.sh    | 134 +++++++++++++++++++++++++-
 3 files changed, 267 insertions(+), 23 deletions(-)

diff --git a/Documentation/git-sparse-checkout.txt b/Documentation/git-sparse-checkout.txt
index 68392d2a56e..53dc17aa77a 100644
--- a/Documentation/git-sparse-checkout.txt
+++ b/Documentation/git-sparse-checkout.txt
@@ -9,7 +9,7 @@ git-sparse-checkout - Reduce your working tree to a subset of tracked files
 SYNOPSIS
 --------
 [verse]
-'git sparse-checkout' (init | list | set | add | reapply | disable) [<options>]
+'git sparse-checkout' (init | list | set | add | reapply | disable | check-rules) [<options>]
 
 
 DESCRIPTION
@@ -135,6 +135,29 @@ paths to pass to a subsequent 'set' or 'add' command.  However,
 the disable command, so the easy restore of calling a plain `init`
 decreased in utility.
 
+'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
 --------
 `git sparse-checkout set MY/DIR1 SUB/DIR2`::
diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
index 5fdc3d9aab5..8243d866817 100644
--- a/builtin/sparse-checkout.c
+++ b/builtin/sparse-checkout.c
@@ -20,7 +20,7 @@
 static const char *empty_base = "";
 
 static char const * const builtin_sparse_checkout_usage[] = {
-	N_("git sparse-checkout (init | list | set | add | reapply | disable) [<options>]"),
+	N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<options>]"),
 	NULL
 };
 
@@ -384,13 +384,7 @@ static int set_config(enum sparse_checkout_mode mode)
 	return 0;
 }
 
-static int update_modes(int *cone_mode, int *sparse_index)
-{
-	int mode, record_mode;
-
-	/* Determine if we need to record the mode; ensure sparse checkout on */
-	record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
-
+static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
 	/* If not specified, use previous definition of cone mode */
 	if (*cone_mode == -1 && core_apply_sparse_checkout)
 		*cone_mode = core_sparse_checkout_cone;
@@ -398,12 +392,21 @@ static int update_modes(int *cone_mode, int *sparse_index)
 	/* Set cone/non-cone mode appropriately */
 	core_apply_sparse_checkout = 1;
 	if (*cone_mode == 1 || *cone_mode == -1) {
-		mode = MODE_CONE_PATTERNS;
 		core_sparse_checkout_cone = 1;
-	} else {
-		mode = MODE_ALL_PATTERNS;
-		core_sparse_checkout_cone = 0;
+		return MODE_CONE_PATTERNS;
 	}
+	core_sparse_checkout_cone = 0;
+	return MODE_ALL_PATTERNS;
+}
+
+static int update_modes(int *cone_mode, int *sparse_index)
+{
+	int mode, record_mode;
+
+	/* Determine if we need to record the mode; ensure sparse checkout on */
+	record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
+
+	mode = update_cone_mode(cone_mode);
 	if (record_mode && set_config(mode))
 		return 1;
 
@@ -547,7 +550,7 @@ static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
 
 static void add_patterns_from_input(struct pattern_list *pl,
 				    int argc, const char **argv,
-				    int use_stdin)
+				    FILE *file)
 {
 	int i;
 	if (core_sparse_checkout_cone) {
@@ -557,9 +560,9 @@ static void add_patterns_from_input(struct pattern_list *pl,
 		hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
 		pl->use_cone_patterns = 1;
 
-		if (use_stdin) {
+		if (file) {
 			struct strbuf unquoted = STRBUF_INIT;
-			while (!strbuf_getline(&line, stdin)) {
+			while (!strbuf_getline(&line, file)) {
 				if (line.buf[0] == '"') {
 					strbuf_reset(&unquoted);
 					if (unquote_c_style(&unquoted, line.buf, NULL))
@@ -581,10 +584,10 @@ static void add_patterns_from_input(struct pattern_list *pl,
 			}
 		}
 	} else {
-		if (use_stdin) {
+		if (file) {
 			struct strbuf line = STRBUF_INIT;
 
-			while (!strbuf_getline(&line, stdin)) {
+			while (!strbuf_getline(&line, file)) {
 				size_t len;
 				char *buf = strbuf_detach(&line, &len);
 				add_pattern(buf, empty_base, 0, pl, 0);
@@ -611,7 +614,8 @@ static void add_patterns_cone_mode(int argc, const char **argv,
 	struct pattern_list existing;
 	char *sparse_filename = get_sparse_checkout_filename();
 
-	add_patterns_from_input(pl, argc, argv, use_stdin);
+	add_patterns_from_input(pl, argc, argv,
+				use_stdin ? stdin : NULL);
 
 	memset(&existing, 0, sizeof(existing));
 	existing.use_cone_patterns = core_sparse_checkout_cone;
@@ -648,7 +652,7 @@ static void add_patterns_literal(int argc, const char **argv,
 					   pl, NULL, 0))
 		die(_("unable to load existing sparse-checkout patterns"));
 	free(sparse_filename);
-	add_patterns_from_input(pl, argc, argv, use_stdin);
+	add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL);
 }
 
 static int modify_pattern_list(int argc, const char **argv, int use_stdin,
@@ -667,7 +671,8 @@ static int modify_pattern_list(int argc, const char **argv, int use_stdin,
 		break;
 
 	case REPLACE:
-		add_patterns_from_input(pl, argc, argv, use_stdin);
+		add_patterns_from_input(pl, argc, argv,
+					use_stdin ? stdin : NULL);
 		break;
 	}
 
@@ -929,6 +934,91 @@ static int sparse_checkout_disable(int argc, const char **argv,
 	return set_config(MODE_NO_PATTERNS);
 }
 
+static char const * const builtin_sparse_checkout_check_rules_usage[] = {
+	N_("git sparse-checkout check-rules [-z] [--skip-checks]"
+	   "[--[no-]cone] [--rules-file <file>]"),
+	NULL
+};
+
+static struct sparse_checkout_check_rules_opts {
+	int cone_mode;
+	int null_termination;
+	char *rules_file;
+} check_rules_opts;
+
+static int check_rules(struct pattern_list *pl, int null_terminated) {
+	struct strbuf line = STRBUF_INIT;
+	struct strbuf unquoted = STRBUF_INIT;
+	char *path;
+	int line_terminator = null_terminated ? 0 : '\n';
+	strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul
+		: strbuf_getline;
+	the_repository->index->sparse_checkout_patterns = pl;
+	while (!getline_fn(&line, stdin)) {
+		path = line.buf;
+		if (!null_terminated && line.buf[0] == '"') {
+			strbuf_reset(&unquoted);
+			if (unquote_c_style(&unquoted, line.buf, NULL))
+				die(_("unable to unquote C-style string '%s'"),
+					line.buf);
+
+			path = unquoted.buf;
+		}
+
+		if (path_in_sparse_checkout(path, the_repository->index))
+			write_name_quoted(path, stdout, line_terminator);
+	}
+	strbuf_release(&line);
+	strbuf_release(&unquoted);
+
+	return 0;
+}
+
+static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix)
+{
+	static struct option builtin_sparse_checkout_check_rules_options[] = {
+		OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
+			 N_("terminate input and output files by a NUL character")),
+		OPT_BOOL(0, "cone", &check_rules_opts.cone_mode,
+			 N_("when used with --rules-file interpret patterns as cone mode patterns")),
+		OPT_FILENAME(0, "rules-file", &check_rules_opts.rules_file,
+			 N_("use patterns in <file> instead of the current ones.")),
+		OPT_END(),
+	};
+
+	FILE *fp;
+	int ret;
+	struct pattern_list pl = {0};
+	char *sparse_filename;
+	check_rules_opts.cone_mode = -1;
+
+	argc = parse_options(argc, argv, prefix,
+			     builtin_sparse_checkout_check_rules_options,
+			     builtin_sparse_checkout_check_rules_usage,
+			     PARSE_OPT_KEEP_UNKNOWN_OPT);
+
+	if (check_rules_opts.rules_file && check_rules_opts.cone_mode < 0)
+		check_rules_opts.cone_mode = 1;
+
+	update_cone_mode(&check_rules_opts.cone_mode);
+	pl.use_cone_patterns = core_sparse_checkout_cone;
+	if (check_rules_opts.rules_file) {
+		fp = xfopen(check_rules_opts.rules_file, "r");
+		add_patterns_from_input(&pl, argc, argv, fp);
+		fclose(fp);
+	} else {
+		sparse_filename = get_sparse_checkout_filename();
+		if (add_patterns_from_file_to_list(sparse_filename, "", 0, &pl,
+						   NULL, 0))
+			die(_("unable to load existing sparse-checkout patterns"));
+		free(sparse_filename);
+	}
+
+	ret = check_rules(&pl, check_rules_opts.null_termination);
+	clear_pattern_list(&pl);
+	return ret;
+}
+
 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
 {
 	parse_opt_subcommand_fn *fn = NULL;
@@ -939,6 +1029,7 @@ int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
 		OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
 		OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
 		OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
+		OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
 		OPT_END(),
 	};
 
diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh
index 7216267aec7..9ceb17f9118 100755
--- a/t/t1091-sparse-checkout-builtin.sh
+++ b/t/t1091-sparse-checkout-builtin.sh
@@ -555,7 +555,7 @@ test_expect_success 'cone mode: set with core.ignoreCase=true' '
 	check_files repo a folder1
 '
 
-test_expect_success 'interaction with submodules' '
+test_expect_success 'setup submodules' '
 	git clone repo super &&
 	(
 		cd super &&
@@ -566,11 +566,22 @@ test_expect_success 'interaction with submodules' '
 		git commit -m "add submodule" &&
 		git sparse-checkout init --cone &&
 		git sparse-checkout set folder1
-	) &&
+	)
+'
+
+test_expect_success 'interaction with submodules' '
 	check_files super a folder1 modules &&
 	check_files super/modules/child a deep folder1 folder2
 '
 
+test_expect_success 'check-rules interaction with submodules' '
+	git -C super ls-tree --name-only -r HEAD >all-files &&
+	git -C super sparse-checkout check-rules >check-rules-matches <all-files &&
+
+	test_i18ngrep ! "modules/" check-rules-matches &&
+	test_i18ngrep "folder1/" check-rules-matches
+'
+
 test_expect_success 'different sparse-checkouts with worktrees' '
 	git -C repo sparse-checkout set --cone deep folder1 &&
 	git -C repo worktree add --detach ../worktree &&
@@ -915,4 +926,123 @@ test_expect_success 'disable fails outside work tree' '
 	test_i18ngrep "this operation must be run in a work tree" err
 '
 
+test_expect_success 'setup clean' '
+	git -C repo clean -fdx
+'
+
+test_expect_success 'check-rules cone mode' '
+	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 &&
+
+	test_i18ngrep "deep/deeper1/deepest/a" check-rules-file &&
+	test_i18ngrep ! "deep/deeper2" check-rules-file &&
+
+	test_cmp check-rules-file ls-files &&
+	test_cmp check-rules-file check-rules-default
+'
+
+test_expect_success 'check-rules non-cone mode' '
+	cat >rules <<-\EOF &&
+	deep/deeper1/deepest/a
+	EOF
+
+	git -C bare ls-tree -r --name-only HEAD >all-files &&
+	git -C bare sparse-checkout check-rules --no-cone --rules-file ../rules\
+		>check-rules-file <all-files &&
+
+	cat rules | git -C repo sparse-checkout set --no-cone --stdin &&
+	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 &&
+
+	cat >expect <<-\EOF &&
+	deep/deeper1/deepest/a
+	EOF
+
+	test_cmp expect check-rules-file &&
+	test_cmp check-rules-file ls-files &&
+	test_cmp check-rules-file check-rules-default
+'
+
+test_expect_success 'check-rules cone mode is default' '
+	cat >rules <<-\EOF &&
+	folder1
+	EOF
+
+	cat >all-files <<-\EOF &&
+	toplevel
+	folder2/file
+	folder1/file
+	EOF
+
+	cat >expect <<-\EOF &&
+	toplevel
+	folder1/file
+	EOF
+
+	git -C repo sparse-checkout set --no-cone &&
+	git -C repo sparse-checkout check-rules \
+		--rules-file ../rules >actual <all-files &&
+
+	git -C bare sparse-checkout check-rules \
+		--rules-file ../rules >actual-bare <all-files &&
+
+	test_cmp expect actual &&
+	test_cmp expect actual-bare
+'
+
+test_expect_success 'check-rules quoting' '
+	cat >rules <<-EOF &&
+	"folder\" a"
+	EOF
+	cat >files <<-EOF &&
+	"folder\" a/file"
+	"folder\" b/file"
+	EOF
+	cat >expect <<-EOF &&
+	"folder\" a/file"
+	EOF
+	git sparse-checkout check-rules --cone \
+		--rules-file rules >actual <files &&
+
+	test_cmp expect actual
+'
+
+test_expect_success 'check-rules null termination' '
+	cat >rules <<-EOF &&
+	"folder\" a"
+	EOF
+
+	lf_to_nul >files <<-EOF &&
+	folder" a/a
+	folder" a/b
+	folder" b/fileQ
+	EOF
+
+	cat >expect <<-EOF &&
+	folder" a/aQfolder" a/bQ
+	EOF
+
+	git sparse-checkout check-rules --cone -z \
+		--rules-file rules >actual.nul <files &&
+	nul_to_q <actual.nul >actual &&
+	echo >>actual &&
+
+	test_cmp expect actual
+'
+
+
 test_done
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag
  2023-03-27  7:55   ` [PATCH v2 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag William Sprent via GitGitGadget
@ 2023-03-27 17:51     ` Junio C Hamano
  2023-04-07 16:16       ` SZEDER Gábor
  0 siblings, 1 reply; 18+ messages in thread
From: Junio C Hamano @ 2023-03-27 17:51 UTC (permalink / raw)
  To: William Sprent via GitGitGadget
  Cc: git, Victoria Dye, Elijah Newren, William Sprent

"William Sprent via GitGitGadget" <gitgitgadget@gmail.com> writes:

> diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
> index c3738154918..5fdc3d9aab5 100644
> --- a/builtin/sparse-checkout.c
> +++ b/builtin/sparse-checkout.c
> @@ -57,6 +57,7 @@ static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
>  	char *sparse_filename;
>  	int res;
>  
> +	setup_work_tree();
>  	if (!core_apply_sparse_checkout)
>  		die(_("this worktree is not sparse"));
> ...
> @@ -898,6 +903,7 @@ static int sparse_checkout_disable(int argc, const char **argv,
>  	 * forcibly return to a dense checkout regardless of initial state.
>  	 */
>  
> +	setup_work_tree();
>  	argc = parse_options(argc, argv, prefix,
>  			     builtin_sparse_checkout_disable_options,
>  			     builtin_sparse_checkout_disable_usage, 0);

I am throwing this out not as "we must tackle this ugliness before
this patch can proceed" but as "this is ugly, I wish somebody can
figure it out in a cleaner way", so do not take this as a blocking
comment or objection, but more as something to think about improving
if possible as a bonus point.

It really is a shame that we have a nice "dispatch" table at the
beginning of the single caller:

        int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
        {
                parse_opt_subcommand_fn *fn = NULL;
                struct option builtin_sparse_checkout_options[] = {
                        OPT_SUBCOMMAND("list", &fn, sparse_checkout_list),
                        OPT_SUBCOMMAND("init", &fn, sparse_checkout_init),
                        OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
                        OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
                        OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
                        OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
                        OPT_END(),
                };

yet we have to sprinkle setup_work_tree() to all of these functions'
implementation.  If we were able to describe which selected ones do
not need the setup call, we could let the parse-options API to look
up the function and then before calling "fn" we could make the setup
call.  That would allow us to maintain the subcommands much nicely.

Thanks.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 0/2] builtin/sparse-checkout: add check-rules command
  2023-03-27  7:55 ` [PATCH v2 " William Sprent via GitGitGadget
  2023-03-27  7:55   ` [PATCH v2 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag William Sprent via GitGitGadget
  2023-03-27  7:55   ` [PATCH v2 2/2] builtin/sparse-checkout: add check-rules command William Sprent via GitGitGadget
@ 2023-04-01 18:49   ` Elijah Newren
  2023-04-03 17:07     ` Junio C Hamano
  2 siblings, 1 reply; 18+ messages in thread
From: Elijah Newren @ 2023-04-01 18:49 UTC (permalink / raw)
  To: William Sprent via GitGitGadget; +Cc: git, Victoria Dye, William Sprent

Hi,

On Mon, Mar 27, 2023 at 12:55 AM William Sprent via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> Hi
>
> This v2 addresses comments from Elijah's review comments.
>
> There's one thing worth highlighting. Elijah pointed out that the the
> "check-rules cone mode is default" test would be stronger if the test itself
> started with a 'git sparse-checkout set --no-cone' to explicitly test that
> the default interpretation of the rules passed with the '--rules-file'
> option is cone mode even though the current checkout is non-cone. I
> implemented this and it exposed that the option did not actually behave that
> way, and that the test only verified the default behaviour of a bare
> repository.
>
> I've modified the logic of the '--rules-file' option such that it defaults
> to cone mode unless combined with '--no-cone', and I've added a line to the
> documentation to make this more explicit.
>
> The alternative would be to have '--rules-file' assume non cone mode when in
> a non cone mode checkout, but I think this depends a bit on "how deprecated"
> non-cone mode is vs. how important it is to have the option behave
> consistently with 'sparse-checkout set' (which respects the current
> checkout).

Ooh, nice catch.

Given that the whole point of check-rules and --rules-file is to
determine how a _different_ sparse checkout would behave, I agree with
your decision here that the current sparse checkout should not affect
how --rules-file functions.

>
> Changes since v1:
>
>  * Explicitly state in documentation that '--rules-file' expects newline
>    separated rules.
>  * Explicitly state in documentation that '-z' does not affect the
>    '--rules-file' input.
>  * Fixup typo where 'When called with the --rules-file <file> flag' was
>    missing "flag".
>  * Fixup behaviour in 'check-rules --rules-file', such that it defaults to
>    accepting cone mode patterns when in a non cone checkout.
>  * Remember to release string buffers in 'check_rules()'.
>  * Explicitly state in documentation that '--rules-file' defaults to cone
>    mode unless combined with '--no-cone'.
>  * Better test that the default of '--rules-file' is to expect '--cone-mode'
>    by running 'check-rules' in a non-cone mode checkout.

This round addresses all my issues, and I agree with the other change
made in this round.  So, v2 is:

Reviewed-by: Elijah Newren <newren@gmail.com>

Thanks for sending this in, William!

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 0/2] builtin/sparse-checkout: add check-rules command
  2023-04-01 18:49   ` [PATCH v2 0/2] " Elijah Newren
@ 2023-04-03 17:07     ` Junio C Hamano
  0 siblings, 0 replies; 18+ messages in thread
From: Junio C Hamano @ 2023-04-03 17:07 UTC (permalink / raw)
  To: Elijah Newren
  Cc: William Sprent via GitGitGadget, git, Victoria Dye,
	William Sprent

Elijah Newren <newren@gmail.com> writes:

> This round addresses all my issues, and I agree with the other change
> made in this round.  So, v2 is:
>
> Reviewed-by: Elijah Newren <newren@gmail.com>
>
> Thanks for sending this in, William!

Thanks.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag
  2023-03-27 17:51     ` Junio C Hamano
@ 2023-04-07 16:16       ` SZEDER Gábor
  2023-04-07 16:38         ` Junio C Hamano
  0 siblings, 1 reply; 18+ messages in thread
From: SZEDER Gábor @ 2023-04-07 16:16 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: William Sprent via GitGitGadget, git, Victoria Dye, Elijah Newren,
	William Sprent

On Mon, Mar 27, 2023 at 10:51:04AM -0700, Junio C Hamano wrote:
> "William Sprent via GitGitGadget" <gitgitgadget@gmail.com> writes:
> 
> > diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
> > index c3738154918..5fdc3d9aab5 100644
> > --- a/builtin/sparse-checkout.c
> > +++ b/builtin/sparse-checkout.c
> > @@ -57,6 +57,7 @@ static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
> >  	char *sparse_filename;
> >  	int res;
> >  
> > +	setup_work_tree();
> >  	if (!core_apply_sparse_checkout)
> >  		die(_("this worktree is not sparse"));
> > ...
> > @@ -898,6 +903,7 @@ static int sparse_checkout_disable(int argc, const char **argv,
> >  	 * forcibly return to a dense checkout regardless of initial state.
> >  	 */
> >  
> > +	setup_work_tree();
> >  	argc = parse_options(argc, argv, prefix,
> >  			     builtin_sparse_checkout_disable_options,
> >  			     builtin_sparse_checkout_disable_usage, 0);
> 
> I am throwing this out not as "we must tackle this ugliness before
> this patch can proceed" but as "this is ugly, I wish somebody can
> figure it out in a cleaner way", so do not take this as a blocking
> comment or objection, but more as something to think about improving
> if possible as a bonus point.
> 
> It really is a shame that we have a nice "dispatch" table at the
> beginning of the single caller:
> 
>         int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
>         {
>                 parse_opt_subcommand_fn *fn = NULL;
>                 struct option builtin_sparse_checkout_options[] = {
>                         OPT_SUBCOMMAND("list", &fn, sparse_checkout_list),
>                         OPT_SUBCOMMAND("init", &fn, sparse_checkout_init),
>                         OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
>                         OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
>                         OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
>                         OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
>                         OPT_END(),
>                 };
> 
> yet we have to sprinkle setup_work_tree() to all of these functions'
> implementation.  If we were able to describe which selected ones do
> not need the setup call, we could let the parse-options API to look
> up the function and then before calling "fn" we could make the setup
> call.  That would allow us to maintain the subcommands much nicely.

It's easy enough to do in this particular case: there is an
OPT_SUBCOMMAND_F() variant which takes an additional flags parameter,
so we could add a PARSE_OPT_SETUP_WORK_TREE flag, check it in e.g.
parse_subcommand(), and act accordingly if it's set.

However, this wouldn't work when the command has a default operation
mode and is invoked without any subcommands.  And I'm not sure about
doing this in parse-options, because it's about, well, parsing
options, not about doing fancy setup stuff.



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag
  2023-04-07 16:16       ` SZEDER Gábor
@ 2023-04-07 16:38         ` Junio C Hamano
  0 siblings, 0 replies; 18+ messages in thread
From: Junio C Hamano @ 2023-04-07 16:38 UTC (permalink / raw)
  To: SZEDER Gábor
  Cc: William Sprent via GitGitGadget, git, Victoria Dye, Elijah Newren,
	William Sprent

SZEDER Gábor <szeder.dev@gmail.com> writes:

>>                 struct option builtin_sparse_checkout_options[] = {
>>                         OPT_SUBCOMMAND("list", &fn, sparse_checkout_list),
>>                         OPT_SUBCOMMAND("init", &fn, sparse_checkout_init),
>>                         OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
>>                         OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
>>                         OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
>>                         OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
>>                         OPT_END(),
>>                 };
>> 
>> yet we have to sprinkle setup_work_tree() to all of these functions'
>> implementation.  If we were able to describe which selected ones do
>> not need the setup call, we could let the parse-options API to look
>> up the function and then before calling "fn" we could make the setup
>> call.  That would allow us to maintain the subcommands much nicely.
>
> It's easy enough to do in this particular case: there is an
> OPT_SUBCOMMAND_F() variant which takes an additional flags parameter,
> so we could add a PARSE_OPT_SETUP_WORK_TREE flag, check it in e.g.
> parse_subcommand(), and act accordingly if it's set.
>
> However, this wouldn't work when the command has a default operation
> mode and is invoked without any subcommands.  And I'm not sure about
> doing this in parse-options, because it's about, well, parsing
> options, not about doing fancy setup stuff.

Yes, exactly.  What I was imagining was more along the lines of

	parse_opt_subcommand_fn *fn = NULL;
	parse_opt_subcommand_fn *fn_with_setup = NULL;
	options[] = {
		OPT_SUBCOMMAND("list", &fn_with_setup, sparse_checkout_list),
		...
		OPT_SUBCOMMAND("check-rules", &fn, sparse_check_rules),
	};

	parse_options(...);

        if (fn_with_setup) {
		setup_worktree();
		fn = fn_with_setup;
	}
	fn(...);

But of course as a "safety" measure, one options[] array can all
point at the same "fn" variable or parse_options() becomes unhappy,
so the above does not work out of the box.

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2023-04-07 16:38 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-08 13:49 [PATCH 0/2] builtin/sparse-checkout: add check-rules command William Sprent via GitGitGadget
2023-03-08 13:49 ` [PATCH 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag William Sprent via GitGitGadget
2023-03-08 18:14   ` Junio C Hamano
2023-03-08 19:27     ` Junio C Hamano
2023-03-09 15:31       ` Elijah Newren
2023-03-09 22:19         ` Junio C Hamano
2023-03-08 13:49 ` [PATCH 2/2] builtin/sparse-checkout: add check-rules command William Sprent via GitGitGadget
2023-03-19  4:26   ` Elijah Newren
2023-03-20 15:49     ` William Sprent
2023-03-19  4:28 ` [PATCH 0/2] " Elijah Newren
2023-03-27  7:55 ` [PATCH v2 " William Sprent via GitGitGadget
2023-03-27  7:55   ` [PATCH v2 1/2] builtin/sparse-checkout: remove NEED_WORK_TREE flag William Sprent via GitGitGadget
2023-03-27 17:51     ` Junio C Hamano
2023-04-07 16:16       ` SZEDER Gábor
2023-04-07 16:38         ` Junio C Hamano
2023-03-27  7:55   ` [PATCH v2 2/2] builtin/sparse-checkout: add check-rules command William Sprent via GitGitGadget
2023-04-01 18:49   ` [PATCH v2 0/2] " Elijah Newren
2023-04-03 17:07     ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).