From: Denton Liu Date: Sun, 14 Jun 2020 08:49:22 GMT Subject: [PATCH v2] checkout: teach --worktree Message-ID: <2a434d328429907a304ad6aaad4c1d9261d27831.1592124367.git.liu.denton@gmail.com> In-Reply-To: A complaint that has come up frequently in the past is that it is not possible to checkout files directly into a worktree without modifying the index. Even though this could be worked around by redirecting the output of `git show` to overwrite files, this was not feasible if one wanted to use patch mode. Since `git restore` was implemented, this has since been possible using the `--worktree` option. However, some long-time users of Git still prefer to use `git checkout` over `git restore` and would like to see the functionality ported over. Teach `git checkout --worktree`, allowing users to checkout files directly into the worktree without affecting the index. Signed-off-by: Denton Liu --- I realised that making `git checkout --worktree` without a pathspec to check out the current directory doesn't really make sense so I didn't make that change. Range-diff against v1: 1: d10cb03dd8 ! 1: 2a434d3284 checkout: teach --worktree @@ Documentation/git-checkout.txt: When switching branches with `--merge`, staged c +-W:: +--worktree:: + When writing contents, only modify files in the worktree. Do not -+ modify the index. This option is essentially a no-op when used -+ without a ``. ++ modify the index. This option does not make sense without a ++ ``. + -p:: --patch:: @@ builtin/checkout.c: static int checkout_main(int argc, const char **argv, const + + opts->checkout_index = 0; + opts->checkout_worktree = 1; ++ opts->empty_pathspec_ok = 0; + + return 0; +} @@ builtin/checkout.c: int cmd_checkout(int argc, const char **argv, const char *pr OPT_STRING('B', NULL, &opts.new_branch_force, N_("branch"), N_("create/reset and checkout a branch")), + OPT_CALLBACK_F('W', "worktree", &opts, NULL, -+ N_("restore the working tree (default)"), ++ N_("restore the working tree"), + PARSE_OPT_NOARG | PARSE_OPT_NONEG, handle_worktree_opt), OPT_BOOL('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")), OPT_BOOL(0, "guess", &opts.dwim_new_local_branch, @@ t/t2028-checkout-worktree.sh (new) + test_cmp_rev HEAD tip +' + ++test_expect_success 'checkout --worktree without pathspec fails' ' ++ test_must_fail git checkout --worktree ++' ++ +test_expect_success 'checkout --no-worktree fails' ' + test_must_fail git checkout --no-worktree +' Documentation/git-checkout.txt | 22 +++++++++----- builtin/checkout.c | 18 +++++++++++ t/t2028-checkout-worktree.sh | 55 ++++++++++++++++++++++++++++++++++ t/t9902-completion.sh | 1 + 4 files changed, 89 insertions(+), 7 deletions(-) create mode 100755 t/t2028-checkout-worktree.sh diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt index 5b697eee1b..c303839920 100644 --- a/Documentation/git-checkout.txt +++ b/Documentation/git-checkout.txt @@ -12,9 +12,9 @@ SYNOPSIS 'git checkout' [-q] [-f] [-m] --detach [] 'git checkout' [-q] [-f] [-m] [--detach] 'git checkout' [-q] [-f] [-m] [[-b|-B|--orphan] ] [] -'git checkout' [-f|--ours|--theirs|-m|--conflict=