diff mbox series

[v6,15/19] sparse-checkout: use in-process update for disable subcommand

Message ID cc6773e49b70681a9efbaa42d5a78011a0cafb38.1574373892.git.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series New sparse-checkout builtin and "cone" mode | expand

Commit Message

Linus Arver via GitGitGadget Nov. 21, 2019, 10:04 p.m. UTC
From: Derrick Stolee <dstolee@microsoft.com>

The 'git sparse-checkout disable' subcommand returns a user to a
full working directory. The old process for doing this required
updating the sparse-checkout file with the "/*" pattern and then
updating the working directory with core.sparseCheckout enabled.
Finally, the sparse-checkout file could be removed and the config
setting disabled.

However, it is valuable to keep a user's sparse-checkout file
intact so they can re-enable the sparse-checkout they previously
used with 'git sparse-checkout init'. This is now possible with
the in-process mechanism for updating the working directory.

Reported-by: Szeder Gábor <szeder.dev@gmail.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 Documentation/git-sparse-checkout.txt |  6 ++++--
 builtin/sparse-checkout.c             | 25 ++++++++++++-------------
 t/t1091-sparse-checkout-builtin.sh    |  3 ++-
 3 files changed, 18 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/Documentation/git-sparse-checkout.txt b/Documentation/git-sparse-checkout.txt
index 8535f0cf40..b975285673 100644
--- a/Documentation/git-sparse-checkout.txt
+++ b/Documentation/git-sparse-checkout.txt
@@ -52,8 +52,10 @@  When the `--stdin` option is provided, the patterns are read from
 standard in as a newline-delimited list instead of from the arguments.
 
 'disable'::
-	Remove the sparse-checkout file, set `core.sparseCheckout` to
-	`false`, and restore the working directory to include all files.
+	Disable the `core.sparseCheckout` config setting, and restore the
+	working directory to include all files. Leaves the sparse-checkout
+	file intact so a later 'git sparse-checkout init' command may
+	return the working directory to the same state.
 
 SPARSE CHECKOUT
 ---------------
diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
index a5d32e4702..a11ea65599 100644
--- a/builtin/sparse-checkout.c
+++ b/builtin/sparse-checkout.c
@@ -412,24 +412,23 @@  static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
 
 static int sparse_checkout_disable(int argc, const char **argv)
 {
-	char *sparse_filename;
-	FILE *fp;
+	static const char *empty_base = "";
+	struct pattern_list pl;
+	struct strbuf match_all = STRBUF_INIT;
 
-	if (set_config(MODE_ALL_PATTERNS))
-		die(_("failed to change config"));
+	memset(&pl, 0, sizeof(pl));
+	hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
+	hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
+	pl.use_cone_patterns = 0;
+	core_apply_sparse_checkout = 1;
 
-	sparse_filename = get_sparse_checkout_filename();
-	fp = xfopen(sparse_filename, "w");
-	fprintf(fp, "/*\n");
-	fclose(fp);
+	strbuf_addstr(&match_all, "/*");
+	add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
 
-	core_apply_sparse_checkout = 1;
-	if (update_working_directory(NULL))
+	if (update_working_directory(&pl))
 		die(_("error while refreshing working directory"));
 
-	unlink(sparse_filename);
-	free(sparse_filename);
-
+	clear_pattern_list(&pl);
 	return set_config(MODE_NO_PATTERNS);
 }
 
diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh
index 53aeb5980f..b8f18e2a09 100755
--- a/t/t1091-sparse-checkout-builtin.sh
+++ b/t/t1091-sparse-checkout-builtin.sh
@@ -172,8 +172,9 @@  test_expect_success 'cone mode: warn on bad pattern' '
 '
 
 test_expect_success 'sparse-checkout disable' '
+	test_when_finished rm -rf repo/.git/info/sparse-checkout &&
 	git -C repo sparse-checkout disable &&
-	test_path_is_missing repo/.git/info/sparse-checkout &&
+	test_path_is_file repo/.git/info/sparse-checkout &&
 	git -C repo config --list >config &&
 	test_must_fail git config core.sparseCheckout &&
 	ls repo >dir &&