checkout: add --ignore-skip-worktree-bits in sparse checkout mode

"git checkout -- <paths>" is usually used to restore all modified files in <paths>. In sparse checkout mode, this command is overloaded with another meaning: to add back all files in <paths> that are excluded by sparse patterns. As the former makes more sense for day-to-day use. Switch it to the default and the latter enabled with --ignore-skip-worktree-bits. While at there, add info/sparse-checkout to gitrepository-layout.txt Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Apr 13, 2013 at 09:12 UTC 08d595dc1cdf6f0d8e6022a69c4fcdd2fba628cf
5 files changed +40 -1
Documentation/git-checkout.txt
+6
@@ -180,6 +180,12 @@ branch by running "git rm -rf ." from the top level of the working tree.
180 Afterwards you will be ready to prepare your new files, repopulating the
181 working tree, by copying them from elsewhere, extracting a tarball, etc.
182
183 +--ignore-skip-worktree-bits::
184 + In sparse checkout mode, `git checkout -- <paths>` would
185 + update only entries matched by <paths> and sparse patterns
186 + in $GIT_DIR/info/sparse-checkout. This option ignores
187 + the sparse patterns and adds back any files in <paths>.
188 +
189 -m::
190 --merge::
191 When switching branches,
Documentation/gitrepository-layout.txt
+4
@@ -184,6 +184,10 @@ info/exclude::
184 'git clean' look at it but the core Git commands do not look
185 at it. See also: linkgit:gitignore[5].
186
187 +info/sparse-checkout::
188 + This file stores sparse checkout patterns.
189 + See also: linkgit:git-read-tree[1].
190 +
191 remotes::
192 Stores shorthands for URL and default refnames for use
193 when interacting with remote repositories via 'git fetch',
builtin/checkout.c
+5
@@ -35,6 +35,7 @@ struct checkout_opts {
35 int force_detach;
36 int writeout_stage;
37 int overwrite_ignore;
38 + int ignore_skipworktree;
39
40 const char *new_branch;
41 const char *new_branch_force;
@@ -278,6 +279,8 @@ static int checkout_paths(const struct checkout_opts *opts,
279 for (pos = 0; pos < active_nr; pos++) {
280 struct cache_entry *ce = active_cache[pos];
281 ce->ce_flags &= ~CE_MATCHED;
282 + if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
283 + continue;
284 if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
285 /*
286 * "git checkout tree-ish -- path", but this entry
@@ -1058,6 +1061,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1061 OPT_STRING(0, "conflict", &conflict_style, N_("style"),
1062 N_("conflict style (merge or diff3)")),
1063 OPT_BOOLEAN('p', "patch", &opts.patch_mode, N_("select hunks interactively")),
1064 + OPT_BOOL(0, "ignore-skip-worktree-bits", &opts.ignore_skipworktree,
1065 + N_("do not limit pathspecs to sparse entries only")),
1066 { OPTION_BOOLEAN, 0, "guess", &dwim_new_local_branch, NULL,
1067 N_("second guess 'git checkout no-such-branch'"),
1068 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
t/t1011-read-tree-sparse-checkout.sh
+24
@@ -250,4 +250,28 @@ EOF
250 test_cmp expected actual
251 '
252
253 +test_expect_success 'checkout without --ignore-skip-worktree-bits' '
254 + echo "*" >.git/info/sparse-checkout &&
255 + git checkout -f top &&
256 + test_path_is_file init.t &&
257 + echo sub >.git/info/sparse-checkout &&
258 + git checkout &&
259 + echo modified >> sub/added &&
260 + git checkout . &&
261 + test_path_is_missing init.t &&
262 + git diff --exit-code HEAD
263 +'
264 +
265 +test_expect_success 'checkout with --ignore-skip-worktree-bits' '
266 + echo "*" >.git/info/sparse-checkout &&
267 + git checkout -f top &&
268 + test_path_is_file init.t &&
269 + echo sub >.git/info/sparse-checkout &&
270 + git checkout &&
271 + echo modified >> sub/added &&
272 + git checkout --ignore-skip-worktree-bits . &&
273 + test_path_is_file init.t &&
274 + git diff --exit-code HEAD
275 +'
276 +
277 test_done
t/t3001-ls-files-others-exclude.sh
+1 -1
@@ -103,7 +103,7 @@ test_expect_success \
103 test_cmp expect output'
104
105 test_expect_success 'restore gitignore' '
106 - git checkout $allignores &&
106 + git checkout --ignore-skip-worktree-bits $allignores &&
107 rm .git/index
108 '
109