Skip to content

Commit de0d774

Browse files
committed
Merge branch 'nd/checkout-keep-sparse'
Make the initial "sparse" selection of the paths more sticky across "git checkout". * nd/checkout-keep-sparse: checkout: add --ignore-skip-worktree-bits in sparse checkout mode
2 parents 7033193 + 08d595d commit de0d774

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

Documentation/git-checkout.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ branch by running "git rm -rf ." from the top level of the working tree.
180180
Afterwards you will be ready to prepare your new files, repopulating the
181181
working tree, by copying them from elsewhere, extracting a tarball, etc.
182182

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+
183189
-m::
184190
--merge::
185191
When switching branches,

Documentation/gitrepository-layout.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ info/exclude::
184184
'git clean' look at it but the core Git commands do not look
185185
at it. See also: linkgit:gitignore[5].
186186

187+
info/sparse-checkout::
188+
This file stores sparse checkout patterns.
189+
See also: linkgit:git-read-tree[1].
190+
187191
remotes::
188192
Stores shorthands for URL and default refnames for use
189193
when interacting with remote repositories via 'git fetch',

builtin/checkout.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct checkout_opts {
3535
int force_detach;
3636
int writeout_stage;
3737
int overwrite_ignore;
38+
int ignore_skipworktree;
3839

3940
const char *new_branch;
4041
const char *new_branch_force;
@@ -278,6 +279,8 @@ static int checkout_paths(const struct checkout_opts *opts,
278279
for (pos = 0; pos < active_nr; pos++) {
279280
struct cache_entry *ce = active_cache[pos];
280281
ce->ce_flags &= ~CE_MATCHED;
282+
if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
283+
continue;
281284
if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
282285
/*
283286
* "git checkout tree-ish -- path", but this entry
@@ -1058,6 +1061,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
10581061
OPT_STRING(0, "conflict", &conflict_style, N_("style"),
10591062
N_("conflict style (merge or diff3)")),
10601063
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")),
10611066
{ OPTION_BOOLEAN, 0, "guess", &dwim_new_local_branch, NULL,
10621067
N_("second guess 'git checkout no-such-branch'"),
10631068
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },

t/t1011-read-tree-sparse-checkout.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,28 @@ EOF
250250
test_cmp expected actual
251251
'
252252

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+
253277
test_done

t/t3001-ls-files-others-exclude.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ test_expect_success \
103103
test_cmp expect output'
104104

105105
test_expect_success 'restore gitignore' '
106-
git checkout $allignores &&
106+
git checkout --ignore-skip-worktree-bits $allignores &&
107107
rm .git/index
108108
'
109109

0 commit comments

Comments
 (0)