Skip to content

Commit

Permalink
checkout-index: add --sparse option
Browse files Browse the repository at this point in the history
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 <vdye@github.com>
  • Loading branch information
vdye authored and ldennington committed Jan 20, 2022
1 parent b8cc9ab commit 84e3b80
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
10 changes: 8 additions & 2 deletions Documentation/git-checkout-index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ SYNOPSIS
'git checkout-index' [-u] [-q] [-a] [-f] [-n] [--prefix=<string>]
[--stage=<number>|all]
[--temp]
[--sparse]
[-z] [--stdin]
[--] [<file>...]

Expand All @@ -37,8 +38,9 @@ OPTIONS

-a::
--all::
checks out all files in the index. Cannot be used
together with explicit filenames.
checks out all files in the index, excluding those outside
any specified sparse checkout patterns (see `--sparse`).
Cannot be used together with explicit filenames.

-n::
--no-create::
Expand All @@ -59,6 +61,10 @@ OPTIONS
write the content to temporary files. The temporary name
associations will be written to stdout.

--sparse::
Refresh files outside of the sparse checkout boundary. May
only be used in conjunction with `--all`.

--stdin::
Instead of taking list of paths from the command line,
read list of paths from the standard input. Paths are
Expand Down
12 changes: 10 additions & 2 deletions builtin/checkout-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define USE_THE_INDEX_COMPATIBILITY_MACROS
#include "builtin.h"
#include "config.h"
#include "dir.h"
#include "lockfile.h"
#include "quote.h"
#include "cache-tree.h"
Expand Down Expand Up @@ -116,7 +117,7 @@ static int checkout_file(const char *name, const char *prefix)
return -1;
}

static int checkout_all(const char *prefix, int prefix_length)
static int checkout_all(const char *prefix, int prefix_length, int include_sparse)
{
int i, errs = 0;
struct cache_entry *last_ce = NULL;
Expand All @@ -125,6 +126,8 @@ static int checkout_all(const char *prefix, int prefix_length)
ensure_full_index(&the_index);
for (i = 0; i < active_nr ; i++) {
struct cache_entry *ce = active_cache[i];
if (!include_sparse && !path_in_sparse_checkout(ce->name, &the_index))
continue;
if (ce_stage(ce) != checkout_stage
&& (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
continue;
Expand Down Expand Up @@ -176,6 +179,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
int i;
struct lock_file lock_file = LOCK_INIT;
int all = 0;
int include_sparse = 0;
int read_from_stdin = 0;
int prefix_length;
int force = 0, quiet = 0, not_new = 0;
Expand All @@ -185,6 +189,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
struct option builtin_checkout_index_options[] = {
OPT_BOOL('a', "all", &all,
N_("check out all files in the index")),
OPT_BOOL(0, "sparse", &include_sparse,
N_("do not skip files outside the sparse checkout boundary")),
OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
OPT__QUIET(&quiet,
N_("no warning for existing files and files not in index")),
Expand Down Expand Up @@ -247,6 +253,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)

if (all)
die("git checkout-index: don't mix '--all' and explicit filenames");
if (include_sparse)
die("git checkout-index: don't mix '--sparse' and explicit filenames");
if (read_from_stdin)
die("git checkout-index: don't mix '--stdin' and explicit filenames");
p = prefix_path(prefix, prefix_length, arg);
Expand Down Expand Up @@ -280,7 +288,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
}

if (all)
err |= checkout_all(prefix, prefix_length);
err |= checkout_all(prefix, prefix_length, include_sparse);

if (pc_workers > 1)
err |= run_parallel_checkout(&state, pc_workers, pc_threshold,
Expand Down
2 changes: 1 addition & 1 deletion builtin/stash.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ static int restore_untracked(struct object_id *u_tree)

child_process_init(&cp);
cp.git_cmd = 1;
strvec_pushl(&cp.args, "checkout-index", "--all", NULL);
strvec_pushl(&cp.args, "checkout-index", "--all", "--sparse", NULL);
strvec_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
stash_index_path.buf);

Expand Down
10 changes: 5 additions & 5 deletions t/t1092-sparse-checkout-compatibility.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1093,14 +1093,14 @@ test_expect_success 'checkout-index with folders' '
test_all_match test_must_fail git checkout-index -f -- folder1/
'

# NEEDSWORK: even in sparse checkouts, checkout-index --all will create all
# files (even those outside the sparse definition) on disk. However, these files
# don't appear in the percentage of tracked files in git status.
test_expect_failure 'checkout-index --all' '
test_expect_success 'checkout-index --all' '
init_repos &&
test_all_match git checkout-index --all &&
test_sparse_match test_path_is_missing folder1
test_sparse_match test_path_is_missing folder1 &&
test_all_match git checkout-index --sparse --all &&
test_all_match test_path_exists folder1
'

test_expect_success 'clean' '
Expand Down

0 comments on commit 84e3b80

Please sign in to comment.