Skip to content

Commit cf3e9f4

Browse files
committed
checkout-index: add --sparse option
Change the default behavior of `checkout-index --all` for sparse checkouts to no longer refresh files outside the sparse checkout definition. The newly-added `--sparse` option, when used with `--all`, maintains the "old" behavior and checks out files outside the sparse checkout definition. Signed-off-by: Victoria Dye <[email protected]>
1 parent 7d45ff5 commit cf3e9f4

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
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+
[--sparse]
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, excluding those outside
42+
any specified sparse checkout patterns (see `--sparse`).
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+
--sparse::
65+
Refresh files outside of the sparse checkout boundary. May
66+
only be used in conjunction with `--all`.
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: 10 additions & 2 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"
@@ -116,7 +117,7 @@ static int checkout_file(const char *name, const char *prefix)
116117
return -1;
117118
}
118119

119-
static int checkout_all(const char *prefix, int prefix_length)
120+
static int checkout_all(const char *prefix, int prefix_length, int include_sparse)
120121
{
121122
int i, errs = 0;
122123
struct cache_entry *last_ce = NULL;
@@ -125,6 +126,8 @@ static int checkout_all(const char *prefix, int prefix_length)
125126
ensure_full_index(&the_index);
126127
for (i = 0; i < active_nr ; i++) {
127128
struct cache_entry *ce = active_cache[i];
129+
if (!include_sparse && !path_in_sparse_checkout(ce->name, &the_index))
130+
continue;
128131
if (ce_stage(ce) != checkout_stage
129132
&& (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
130133
continue;
@@ -176,6 +179,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
176179
int i;
177180
struct lock_file lock_file = LOCK_INIT;
178181
int all = 0;
182+
int include_sparse = 0;
179183
int read_from_stdin = 0;
180184
int prefix_length;
181185
int force = 0, quiet = 0, not_new = 0;
@@ -185,6 +189,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
185189
struct option builtin_checkout_index_options[] = {
186190
OPT_BOOL('a', "all", &all,
187191
N_("check out all files in the index")),
192+
OPT_BOOL(0, "sparse", &include_sparse,
193+
N_("do not skip files outside the sparse checkout boundary")),
188194
OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
189195
OPT__QUIET(&quiet,
190196
N_("no warning for existing files and files not in index")),
@@ -247,6 +253,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
247253

248254
if (all)
249255
die("git checkout-index: don't mix '--all' and explicit filenames");
256+
if (include_sparse)
257+
die("git checkout-index: don't mix '--sparse' and explicit filenames");
250258
if (read_from_stdin)
251259
die("git checkout-index: don't mix '--stdin' and explicit filenames");
252260
p = prefix_path(prefix, prefix_length, arg);
@@ -280,7 +288,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
280288
}
281289

282290
if (all)
283-
err |= checkout_all(prefix, prefix_length);
291+
err |= checkout_all(prefix, prefix_length, include_sparse);
284292

285293
if (pc_workers > 1)
286294
err |= run_parallel_checkout(&state, pc_workers, pc_threshold,

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -927,14 +927,14 @@ test_expect_success 'checkout-index with folders' '
927927
test_all_match test_must_fail git checkout-index -f -- folder1/
928928
'
929929

930-
# NEEDSWORK: even in sparse checkouts, checkout-index --all will create all
931-
# files (even those outside the sparse definition) on disk. However, these files
932-
# don't appear in the percentage of tracked files in git status.
933-
test_expect_failure 'checkout-index --all' '
930+
test_expect_success 'checkout-index --all' '
934931
init_repos &&
935932
936933
test_all_match git checkout-index --all &&
937-
test_sparse_match test_path_is_missing folder1
934+
test_sparse_match test_path_is_missing folder1 &&
935+
936+
test_all_match git checkout-index --sparse --all &&
937+
test_all_match test_path_exists folder1
938938
'
939939

940940
test_expect_success 'clean' '

0 commit comments

Comments
 (0)