Skip to content

Commit 88078f5

Browse files
vdyegitster
authored andcommitted
checkout-index: add --ignore-skip-worktree-bits option
Update `checkout-index` to no longer refresh files that have the `skip-worktree` bit set, exiting with an error if `skip-worktree` filenames are directly provided to `checkout-index`. The newly-added `--ignore-skip-worktree-bits` option provides a mechanism to replicate the old behavior, checking out *all* files specified (even those with `skip-worktree` enabled). The ability to toggle whether files should be checked-out based on `skip-worktree` already exists in `git checkout` and `git restore` (both of which have an `--ignore-skip-worktree-bits` option). The change to, by default, ignore `skip-worktree` files is especially helpful for sparse-checkout; it prevents inadvertent creation of files outside the sparse definition on disk and eliminates the need to expand a sparse index when using the `--all` option. Internal usage of `checkout-index` in `git stash` and `git filter-branch` do not make explicit use of files with `skip-worktree` enabled, so `--ignore-skip-worktree-bits` is not added to them. Helped-by: Elijah Newren <[email protected]> Signed-off-by: Victoria Dye <[email protected]> Reviewed-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b553ef6 commit 88078f5

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

Documentation/git-checkout-index.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ SYNOPSIS
1212
'git checkout-index' [-u] [-q] [-a] [-f] [-n] [--prefix=<string>]
1313
[--stage=<number>|all]
1414
[--temp]
15+
[--ignore-skip-worktree-bits]
1516
[-z] [--stdin]
1617
[--] [<file>...]
1718

@@ -37,8 +38,9 @@ OPTIONS
3738

3839
-a::
3940
--all::
40-
checks out all files in the index. Cannot be used
41-
together with explicit filenames.
41+
checks out all files in the index except for those with the
42+
skip-worktree bit set (see `--ignore-skip-worktree-bits`).
43+
Cannot be used together with explicit filenames.
4244

4345
-n::
4446
--no-create::
@@ -59,6 +61,10 @@ OPTIONS
5961
write the content to temporary files. The temporary name
6062
associations will be written to stdout.
6163

64+
--ignore-skip-worktree-bits::
65+
Check out all files, including those with the skip-worktree bit
66+
set.
67+
6268
--stdin::
6369
Instead of taking list of paths from the command line,
6470
read list of paths from the standard input. Paths are

builtin/checkout-index.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define USE_THE_INDEX_COMPATIBILITY_MACROS
88
#include "builtin.h"
99
#include "config.h"
10+
#include "dir.h"
1011
#include "lockfile.h"
1112
#include "quote.h"
1213
#include "cache-tree.h"
@@ -17,6 +18,7 @@
1718
#define CHECKOUT_ALL 4
1819
static int nul_term_line;
1920
static int checkout_stage; /* default to checkout stage0 */
21+
static int ignore_skip_worktree; /* default to 0 */
2022
static int to_tempfile;
2123
static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
2224

@@ -65,6 +67,7 @@ static int checkout_file(const char *name, const char *prefix)
6567
int namelen = strlen(name);
6668
int pos = cache_name_pos(name, namelen);
6769
int has_same_name = 0;
70+
int is_skipped = 1;
6871
int did_checkout = 0;
6972
int errs = 0;
7073

@@ -78,6 +81,9 @@ static int checkout_file(const char *name, const char *prefix)
7881
break;
7982
has_same_name = 1;
8083
pos++;
84+
if (!ignore_skip_worktree && ce_skip_worktree(ce))
85+
break;
86+
is_skipped = 0;
8187
if (ce_stage(ce) != checkout_stage
8288
&& (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
8389
continue;
@@ -106,6 +112,9 @@ static int checkout_file(const char *name, const char *prefix)
106112
fprintf(stderr, "git checkout-index: %s ", name);
107113
if (!has_same_name)
108114
fprintf(stderr, "is not in the cache");
115+
else if (is_skipped)
116+
fprintf(stderr, "has skip-worktree enabled; "
117+
"use '--ignore-skip-worktree-bits' to checkout");
109118
else if (checkout_stage)
110119
fprintf(stderr, "does not exist at stage %d",
111120
checkout_stage);
@@ -125,6 +134,8 @@ static int checkout_all(const char *prefix, int prefix_length)
125134
ensure_full_index(&the_index);
126135
for (i = 0; i < active_nr ; i++) {
127136
struct cache_entry *ce = active_cache[i];
137+
if (!ignore_skip_worktree && ce_skip_worktree(ce))
138+
continue;
128139
if (ce_stage(ce) != checkout_stage
129140
&& (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
130141
continue;
@@ -185,6 +196,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
185196
struct option builtin_checkout_index_options[] = {
186197
OPT_BOOL('a', "all", &all,
187198
N_("check out all files in the index")),
199+
OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree,
200+
N_("do not skip files with skip-worktree set")),
188201
OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
189202
OPT__QUIET(&quiet,
190203
N_("no warning for existing files and files not in index")),

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -772,9 +772,14 @@ test_expect_success 'checkout-index inside sparse definition' '
772772
test_expect_success 'checkout-index outside sparse definition' '
773773
init_repos &&
774774
775-
# File does not exist on disk yet for sparse checkouts, so checkout-index
776-
# succeeds without -f
777-
test_sparse_match git checkout-index -- folder1/a &&
775+
# Without --ignore-skip-worktree-bits, outside-of-cone files will trigger
776+
# an error
777+
test_sparse_match test_must_fail git checkout-index -- folder1/a &&
778+
test_i18ngrep "folder1/a has skip-worktree enabled" sparse-checkout-err &&
779+
test_path_is_missing folder1/a &&
780+
781+
# With --ignore-skip-worktree-bits, outside-of-cone files are checked out
782+
test_sparse_match git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
778783
test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
779784
test_cmp sparse-checkout/folder1/a full-checkout/folder1/a &&
780785
@@ -783,8 +788,8 @@ test_expect_success 'checkout-index outside sparse definition' '
783788
run_on_sparse mkdir -p folder1 &&
784789
run_on_all cp ../new-a folder1/a &&
785790
786-
test_all_match test_must_fail git checkout-index -- folder1/a &&
787-
test_all_match git checkout-index -f -- folder1/a &&
791+
test_all_match test_must_fail git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
792+
test_all_match git checkout-index -f --ignore-skip-worktree-bits -- folder1/a &&
788793
test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
789794
test_cmp sparse-checkout/folder1/a full-checkout/folder1/a
790795
'
@@ -799,14 +804,16 @@ test_expect_success 'checkout-index with folders' '
799804
test_all_match test_must_fail git checkout-index -f -- folder1/
800805
'
801806

802-
# NEEDSWORK: even in sparse checkouts, checkout-index --all will create all
803-
# files (even those outside the sparse definition) on disk. However, these files
804-
# don't appear in the percentage of tracked files in git status.
805-
test_expect_failure 'checkout-index --all' '
807+
test_expect_success 'checkout-index --all' '
806808
init_repos &&
807809
808810
test_all_match git checkout-index --all &&
809-
test_sparse_match test_path_is_missing folder1
811+
test_sparse_match test_path_is_missing folder1 &&
812+
813+
# --ignore-skip-worktree-bits will cause `skip-worktree` files to be
814+
# checked out, causing the outside-of-cone `folder1` to exist on-disk
815+
test_all_match git checkout-index --ignore-skip-worktree-bits --all &&
816+
test_all_match test_path_exists folder1
810817
'
811818

812819
test_expect_success 'clean' '

0 commit comments

Comments
 (0)