diff options
author | Junio C Hamano <gitster@pobox.com> | 2017-04-27 09:32:19 +0900 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-04-27 09:32:19 +0900 |
commit | af827b63f8c2886725683f1f538a4163beb046dd (patch) | |
tree | 4b28ae12901cd3bdcd10fa4d6d75d9baa4811003 | |
parent | 49800c940790cc7465d1b03e08d472ffd8684808 (diff) | |
download | git-jc/checkout-working-tree-only.tar.gz |
checkout: add --working-tree-only optionjc/checkout-working-tree-only
"git checkout <tree-ish> <pathspec>" has always copied the blob from
the tree-ish to the index before checking them out to the working tree.
Some users may want to grab a blob out of a tree-ish directly to the
working tree, without updating the index, so that "git diff" can be
used to assess the damage and adjust the file contents taken from a
different branch to be more appropriate for the current branch.
The new option "--working-tree-only" allows exactly that.
In the hindsight, when a command works on the working tree and/or
the index, the usual convention is:
- with no other option, the command works only on the working tree;
- with "--cached" option, the command works only on the index; and
- with "--index" option, the command works on both the working tree
and the index.
So we probably should have triggered the default behaviour under the
"--index" option, and triggered this "--working-tree-only" mode of
behaviour when "--index" option is not given. From the same point
of view, "git checkout --cached <tree-ish> <pathspec>" would have
done the same as "git reset <tree-ish> <pathspec>" would do. And
that may have made the command set a bit more consistent.
But that is merely a hindsight being 20/20, oh well.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r-- | Documentation/git-checkout.txt | 22 | ||||
-rw-r--r-- | builtin/checkout.c | 9 | ||||
-rwxr-xr-x | t/t2022-checkout-paths.sh | 20 |
3 files changed, 43 insertions, 8 deletions
diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt index 8e2c0662dd..201677752e 100644 --- a/Documentation/git-checkout.txt +++ b/Documentation/git-checkout.txt @@ -14,6 +14,7 @@ SYNOPSIS 'git checkout' [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>] 'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>... 'git checkout' [-p|--patch] [<tree-ish>] [--] [<paths>...] +'git checkout' --working-tree-only <tree-ish> [--] [<paths>...] DESCRIPTION ----------- @@ -81,13 +82,14 @@ Omitting <branch> detaches HEAD at the tip of the current branch. 'git checkout' [-p|--patch] [<tree-ish>] [--] <pathspec>...:: When <paths> or `--patch` are given, 'git checkout' does *not* - switch branches. It updates the named paths in the working tree - from the index file or from a named <tree-ish> (most often a - commit). In this case, the `-b` and `--track` options are - meaningless and giving either of them results in an error. The - <tree-ish> argument can be used to specify a specific tree-ish - (i.e. commit, tag or tree) to update the index for the given - paths before updating the working tree. + switch branches. In this case, the `-b` and `--track` options + are meaningless and giving either of them results in an error. ++ +The command checks out blobs for paths that match the given +<pathspec> from the index to the working tree. When an optional +<tree-ish> is given, the blobs for paths that match the given +<pathspec> are copied from the <tree-ish> to the index before +they are checked out of the index. + 'git checkout' with <paths> or `--patch` is used to restore modified or deleted paths to their original contents from the index or replace paths @@ -101,6 +103,12 @@ specific side of the merge can be checked out of the index by using `--ours` or `--theirs`. With `-m`, changes made to the working tree file can be discarded to re-create the original conflicted merge result. +'git checkout' --working-tree-only <tree-ish> [--] <pathspec>...:: + Similar to `git checkout <tree-ish> [--] <pathspec>`, but + the index file is left in the same state as it was before + running this command. + + OPTIONS ------- -q:: diff --git a/builtin/checkout.c b/builtin/checkout.c index 81f07c3ef2..5d583d6c96 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -37,6 +37,7 @@ struct checkout_opts { int overwrite_ignore; int ignore_skipworktree; int ignore_other_worktrees; + int no_index; int show_progress; const char *new_branch; @@ -268,6 +269,9 @@ static int checkout_paths(const struct checkout_opts *opts, die(_("Cannot update paths and switch to branch '%s' at the same time."), opts->new_branch); + if (opts->no_index && !opts->source_tree) + die(_("'--working-tree-only' requires a tree-ish")); + if (opts->patch_mode) return run_add_interactive(revision, "--patch=checkout", &opts->pathspec); @@ -370,7 +374,9 @@ static int checkout_paths(const struct checkout_opts *opts, } } - if (write_locked_index(&the_index, lock_file, COMMIT_LOCK)) + if (opts->no_index) + ; /* discard the in-core index */ + else if (write_locked_index(&the_index, lock_file, COMMIT_LOCK)) die(_("unable to write new index file")); read_ref_full("HEAD", 0, rev.hash, NULL); @@ -1164,6 +1170,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix) OPT_BOOL(0, "ignore-other-worktrees", &opts.ignore_other_worktrees, N_("do not check if another worktree is holding the given ref")), OPT_BOOL(0, "progress", &opts.show_progress, N_("force progress reporting")), + OPT_BOOL(0, "working-tree-only", &opts.no_index, N_("checkout to working tree only without touching the index")), OPT_END(), }; diff --git a/t/t2022-checkout-paths.sh b/t/t2022-checkout-paths.sh index f46d0499bc..9ad74cebb2 100755 --- a/t/t2022-checkout-paths.sh +++ b/t/t2022-checkout-paths.sh @@ -78,4 +78,24 @@ test_expect_success 'do not touch files that are already up-to-date' ' test_cmp expect actual ' +test_expect_success 'working-tree-only option leaves checked out files unadded' ' + git reset --hard && + git checkout -b pu next && + echo another line >>file1 && + echo exists >file3 && + git add file3 && + git commit -a -m "another commit" && + git checkout next && + + ! grep "another line" file1 && + git checkout --working-tree-only pu file1 file3 && + grep "another line" file1 && + test_must_fail git grep --cached "another line" file1 && + + grep exists file3 && + git ls-files file3 >actual && + >expect && + test_cmp expect actual +' + test_done |