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 <newren@gmail.com> Signed-off-by: Victoria Dye <vdye@github.com> Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Jan 11, 2022 at 18:05 UTC 88078f543b769dc13ae9796372651178584a25a0
3 files changed +38 -12
Documentation/git-checkout-index.txt
+8 -2
@@ -12,6 +12,7 @@ SYNOPSIS
12 'git checkout-index' [-u] [-q] [-a] [-f] [-n] [--prefix=<string>]
13 [--stage=<number>|all]
14 [--temp]
15 + [--ignore-skip-worktree-bits]
16 [-z] [--stdin]
17 [--] [<file>...]
18
@@ -37,8 +38,9 @@ OPTIONS
38
39 -a::
40 --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.
44
45 -n::
46 --no-create::
@@ -59,6 +61,10 @@ OPTIONS
61 write the content to temporary files. The temporary name
62 associations will be written to stdout.
63
64 +--ignore-skip-worktree-bits::
65 + Check out all files, including those with the skip-worktree bit
66 + set.
67 +
68 --stdin::
69 Instead of taking list of paths from the command line,
70 read list of paths from the standard input. Paths are
builtin/checkout-index.c
+13
@@ -7,6 +7,7 @@
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
8 #include "builtin.h"
9 #include "config.h"
10 +#include "dir.h"
11 #include "lockfile.h"
12 #include "quote.h"
13 #include "cache-tree.h"
@@ -17,6 +18,7 @@
18 #define CHECKOUT_ALL 4
19 static int nul_term_line;
20 static int checkout_stage; /* default to checkout stage0 */
21 +static int ignore_skip_worktree; /* default to 0 */
22 static int to_tempfile;
23 static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
24
@@ -65,6 +67,7 @@ static int checkout_file(const char *name, const char *prefix)
67 int namelen = strlen(name);
68 int pos = cache_name_pos(name, namelen);
69 int has_same_name = 0;
70 + int is_skipped = 1;
71 int did_checkout = 0;
72 int errs = 0;
73
@@ -78,6 +81,9 @@ static int checkout_file(const char *name, const char *prefix)
81 break;
82 has_same_name = 1;
83 pos++;
84 + if (!ignore_skip_worktree && ce_skip_worktree(ce))
85 + break;
86 + is_skipped = 0;
87 if (ce_stage(ce) != checkout_stage
88 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
89 continue;
@@ -106,6 +112,9 @@ static int checkout_file(const char *name, const char *prefix)
112 fprintf(stderr, "git checkout-index: %s ", name);
113 if (!has_same_name)
114 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");
118 else if (checkout_stage)
119 fprintf(stderr, "does not exist at stage %d",
120 checkout_stage);
@@ -125,6 +134,8 @@ static int checkout_all(const char *prefix, int prefix_length)
134 ensure_full_index(&the_index);
135 for (i = 0; i < active_nr ; i++) {
136 struct cache_entry *ce = active_cache[i];
137 + if (!ignore_skip_worktree && ce_skip_worktree(ce))
138 + continue;
139 if (ce_stage(ce) != checkout_stage
140 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
141 continue;
@@ -185,6 +196,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
196 struct option builtin_checkout_index_options[] = {
197 OPT_BOOL('a', "all", &all,
198 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")),
201 OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
202 OPT__QUIET(&quiet,
203 N_("no warning for existing files and files not in index")),
t/t1092-sparse-checkout-compatibility.sh
+17 -10
@@ -772,9 +772,14 @@ test_expect_success 'checkout-index inside sparse definition' '
772 test_expect_success 'checkout-index outside sparse definition' '
773 init_repos &&
774
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 &&
783 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
784 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a &&
785
@@ -783,8 +788,8 @@ test_expect_success 'checkout-index outside sparse definition' '
788 run_on_sparse mkdir -p folder1 &&
789 run_on_all cp ../new-a folder1/a &&
790
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 &&
793 test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
794 test_cmp sparse-checkout/folder1/a full-checkout/folder1/a
795 '
@@ -799,14 +804,16 @@ test_expect_success 'checkout-index with folders' '
804 test_all_match test_must_fail git checkout-index -f -- folder1/
805 '
806
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' '
808 init_repos &&
809
810 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
817 '
818
819 test_expect_success 'clean' '