Skip to content

Commit 24fc2cd

Browse files
williams-unitygitster
authored andcommitted
builtin/sparse-checkout: remove NEED_WORK_TREE flag
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 27d43aa commit 24fc2cd

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

builtin/sparse-checkout.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
5757
char *sparse_filename;
5858
int res;
5959

60+
setup_work_tree();
6061
if (!core_apply_sparse_checkout)
6162
die(_("this worktree is not sparse"));
6263

@@ -446,6 +447,7 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
446447
OPT_END(),
447448
};
448449

450+
setup_work_tree();
449451
repo_read_index(the_repository);
450452

451453
init_opts.cone_mode = -1;
@@ -758,6 +760,7 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
758760
OPT_END(),
759761
};
760762

763+
setup_work_tree();
761764
if (!core_apply_sparse_checkout)
762765
die(_("no sparse-checkout to add to"));
763766

@@ -804,6 +807,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
804807
OPT_END(),
805808
};
806809

810+
setup_work_tree();
807811
repo_read_index(the_repository);
808812

809813
set_opts.cone_mode = -1;
@@ -853,6 +857,7 @@ static int sparse_checkout_reapply(int argc, const char **argv,
853857
OPT_END(),
854858
};
855859

860+
setup_work_tree();
856861
if (!core_apply_sparse_checkout)
857862
die(_("must be in a sparse-checkout to reapply sparsity patterns"));
858863

@@ -896,6 +901,7 @@ static int sparse_checkout_disable(int argc, const char **argv,
896901
* forcibly return to a dense checkout regardless of initial state.
897902
*/
898903

904+
setup_work_tree();
899905
argc = parse_options(argc, argv, prefix,
900906
builtin_sparse_checkout_disable_options,
901907
builtin_sparse_checkout_disable_usage, 0);

git.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ static struct cmd_struct commands[] = {
584584
{ "show-branch", cmd_show_branch, RUN_SETUP },
585585
{ "show-index", cmd_show_index, RUN_SETUP_GENTLY },
586586
{ "show-ref", cmd_show_ref, RUN_SETUP },
587-
{ "sparse-checkout", cmd_sparse_checkout, RUN_SETUP | NEED_WORK_TREE },
587+
{ "sparse-checkout", cmd_sparse_checkout, RUN_SETUP },
588588
{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
589589
{ "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE },
590590
{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },

t/t1091-sparse-checkout-builtin.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,4 +882,37 @@ test_expect_success 'by default, non-cone mode will warn on individual files' '
882882
grep "pass a leading slash before paths.*if you want a single file" warning
883883
'
884884

885+
test_expect_success 'setup bare repo' '
886+
git clone --bare "file://$(pwd)/repo" bare
887+
'
888+
test_expect_success 'list fails outside work tree' '
889+
test_must_fail git -C bare sparse-checkout list 2>err &&
890+
test_i18ngrep "this operation must be run in a work tree" err
891+
'
892+
893+
test_expect_success 'add fails outside work tree' '
894+
test_must_fail git -C bare sparse-checkout add deeper 2>err &&
895+
test_i18ngrep "this operation must be run in a work tree" err
896+
'
897+
898+
test_expect_success 'set fails outside work tree' '
899+
test_must_fail git -C bare sparse-checkout set deeper 2>err &&
900+
test_i18ngrep "this operation must be run in a work tree" err
901+
'
902+
903+
test_expect_success 'init fails outside work tree' '
904+
test_must_fail git -C bare sparse-checkout init 2>err &&
905+
test_i18ngrep "this operation must be run in a work tree" err
906+
'
907+
908+
test_expect_success 'reapply fails outside work tree' '
909+
test_must_fail git -C bare sparse-checkout reapply 2>err &&
910+
test_i18ngrep "this operation must be run in a work tree" err
911+
'
912+
913+
test_expect_success 'disable fails outside work tree' '
914+
test_must_fail git -C bare sparse-checkout disable 2>err &&
915+
test_i18ngrep "this operation must be run in a work tree" err
916+
'
917+
885918
test_done

0 commit comments

Comments
 (0)