@matheusbsbs / matheus-bagliano / commits / cff4e9138d
Fork 0

sparse-checkout: check for dirty status

The index-merge performed by 'git sparse-checkout' will erase any staged changes, which can lead to data loss. Prevent these attempts by requiring a clean 'git status' output. Helped-by: Szeder Gábor <szeder.dev@gmail.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Nov 21, 2019 at 22:04 UTC cff4e9138d8df45e3b6199171092ee781cdadaeb
2 files changed +27 -1
builtin/sparse-checkout.c
+13
@@ -12,6 +12,7 @@
12 #include "lockfile.h"
13 #include "resolve-undo.h"
14 #include "unpack-trees.h"
15 +#include "wt-status.h"
16
17 static const char *empty_base = "";
18
@@ -256,6 +257,10 @@ static int sparse_checkout_init(int argc, const char **argv)
257 OPT_END(),
258 };
259
260 + repo_read_index(the_repository);
261 + require_clean_work_tree(the_repository,
262 + N_("initialize sparse-checkout"), NULL, 1, 0);
263 +
264 argc = parse_options(argc, argv, NULL,
265 builtin_sparse_checkout_init_options,
266 builtin_sparse_checkout_init_usage, 0);
@@ -368,6 +373,10 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
373 OPT_END(),
374 };
375
376 + repo_read_index(the_repository);
377 + require_clean_work_tree(the_repository,
378 + N_("set sparse-checkout patterns"), NULL, 1, 0);
379 +
380 memset(&pl, 0, sizeof(pl));
381
382 argc = parse_options(argc, argv, prefix,
@@ -427,6 +436,10 @@ static int sparse_checkout_disable(int argc, const char **argv)
436 struct pattern_list pl;
437 struct strbuf match_all = STRBUF_INIT;
438
439 + repo_read_index(the_repository);
440 + require_clean_work_tree(the_repository,
441 + N_("disable sparse-checkout"), NULL, 1, 0);
442 +
443 memset(&pl, 0, sizeof(pl));
444 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
445 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
t/t1091-sparse-checkout-builtin.sh
+14 -1
@@ -250,10 +250,11 @@ test_expect_success 'cone mode: set with nested folders' '
250 '
251
252 test_expect_success 'revert to old sparse-checkout on bad update' '
253 + test_when_finished git -C repo reset --hard &&
254 echo update >repo/deep/deeper2/a &&
255 cp repo/.git/info/sparse-checkout expect &&
256 test_must_fail git -C repo sparse-checkout set deep/deeper1 2>err &&
256 - test_i18ngrep "Cannot update sparse checkout" err &&
257 + test_i18ngrep "cannot set sparse-checkout patterns" err &&
258 test_cmp repo/.git/info/sparse-checkout expect &&
259 ls repo/deep >dir &&
260 cat >expect <<-EOF &&
@@ -291,4 +292,16 @@ test_expect_success '.gitignore should not warn about cone mode' '
292 test_i18ngrep ! "disabling cone patterns" err
293 '
294
295 +test_expect_success 'sparse-checkout (init|set|disable) fails with dirty status' '
296 + git clone repo dirty &&
297 + echo dirty >dirty/folder1/a &&
298 + test_must_fail git -C dirty sparse-checkout init &&
299 + test_must_fail git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
300 + test_must_fail git -C dirty sparse-checkout disable &&
301 + git -C dirty reset --hard &&
302 + git -C dirty sparse-checkout init &&
303 + git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
304 + git -C dirty sparse-checkout disable
305 +'
306 +
307 test_done