summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cache-tree.c3
-rw-r--r--cache.h2
-rw-r--r--read-cache.c26
-rw-r--r--sparse-index.c139
-rw-r--r--sparse-index.h1
-rwxr-xr-xt/t1092-sparse-checkout-compatibility.sh61
6 files changed, 228 insertions, 4 deletions
diff --git a/cache-tree.c b/cache-tree.c
index 2fb483d3c0..5f07a39e50 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -6,6 +6,7 @@
#include "object-store.h"
#include "replace-object.h"
#include "promisor-remote.h"
+#include "sparse-index.h"
#ifndef DEBUG_CACHE_TREE
#define DEBUG_CACHE_TREE 0
@@ -442,6 +443,8 @@ int cache_tree_update(struct index_state *istate, int flags)
if (i)
return i;
+ ensure_full_index(istate);
+
if (!istate->cache_tree)
istate->cache_tree = cache_tree();
diff --git a/cache.h b/cache.h
index 8c4464420d..74b43aaa2b 100644
--- a/cache.h
+++ b/cache.h
@@ -251,6 +251,8 @@ static inline unsigned int create_ce_mode(unsigned int mode)
{
if (S_ISLNK(mode))
return S_IFLNK;
+ if (S_ISSPARSEDIR(mode))
+ return S_IFDIR;
if (S_ISDIR(mode) || S_ISGITLINK(mode))
return S_IFGITLINK;
return S_IFREG | ce_permissions(mode);
diff --git a/read-cache.c b/read-cache.c
index b8f092d1b7..2410e6e0df 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -25,6 +25,7 @@
#include "fsmonitor.h"
#include "thread-utils.h"
#include "progress.h"
+#include "sparse-index.h"
/* Mask for the name length in ce_flags in the on-disk index */
@@ -1003,8 +1004,14 @@ inside:
c = *path++;
if ((c == '.' && !verify_dotfile(path, mode)) ||
- is_dir_sep(c) || c == '\0')
+ is_dir_sep(c))
return 0;
+ /*
+ * allow terminating directory separators for
+ * sparse directory entries.
+ */
+ if (c == '\0')
+ return S_ISDIR(mode);
} else if (c == '\\' && protect_ntfs) {
if (is_ntfs_dotgit(path))
return 0;
@@ -3088,6 +3095,14 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l
unsigned flags)
{
int ret;
+ int was_full = !istate->sparse_index;
+
+ ret = convert_to_sparse(istate);
+
+ if (ret) {
+ warning(_("failed to convert to a sparse-index"));
+ return ret;
+ }
/*
* TODO trace2: replace "the_repository" with the actual repo instance
@@ -3099,6 +3114,9 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l
trace2_region_leave_printf("index", "do_write_index", the_repository,
"%s", get_lock_file_path(lock));
+ if (was_full)
+ ensure_full_index(istate);
+
if (ret)
return ret;
if (flags & COMMIT_LOCK)
@@ -3189,9 +3207,10 @@ static int write_shared_index(struct index_state *istate,
struct tempfile **temp)
{
struct split_index *si = istate->split_index;
- int ret;
+ int ret, was_full = !istate->sparse_index;
move_cache_to_base_index(istate);
+ convert_to_sparse(istate);
trace2_region_enter_printf("index", "shared/do_write_index",
the_repository, "%s", get_tempfile_path(*temp));
@@ -3199,6 +3218,9 @@ static int write_shared_index(struct index_state *istate,
trace2_region_leave_printf("index", "shared/do_write_index",
the_repository, "%s", get_tempfile_path(*temp));
+ if (was_full)
+ ensure_full_index(istate);
+
if (ret)
return ret;
ret = adjust_shared_perm(get_tempfile_path(*temp));
diff --git a/sparse-index.c b/sparse-index.c
index 7095378a1b..619ff7c2e2 100644
--- a/sparse-index.c
+++ b/sparse-index.c
@@ -4,6 +4,145 @@
#include "tree.h"
#include "pathspec.h"
#include "trace2.h"
+#include "cache-tree.h"
+#include "config.h"
+#include "dir.h"
+#include "fsmonitor.h"
+
+static struct cache_entry *construct_sparse_dir_entry(
+ struct index_state *istate,
+ const char *sparse_dir,
+ struct cache_tree *tree)
+{
+ struct cache_entry *de;
+
+ de = make_cache_entry(istate, S_IFDIR, &tree->oid, sparse_dir, 0, 0);
+
+ de->ce_flags |= CE_SKIP_WORKTREE;
+ return de;
+}
+
+/*
+ * Returns the number of entries "inserted" into the index.
+ */
+static int convert_to_sparse_rec(struct index_state *istate,
+ int num_converted,
+ int start, int end,
+ const char *ct_path, size_t ct_pathlen,
+ struct cache_tree *ct)
+{
+ int i, can_convert = 1;
+ int start_converted = num_converted;
+ enum pattern_match_result match;
+ int dtype;
+ struct strbuf child_path = STRBUF_INIT;
+ struct pattern_list *pl = istate->sparse_checkout_patterns;
+
+ /*
+ * Is the current path outside of the sparse cone?
+ * Then check if the region can be replaced by a sparse
+ * directory entry (everything is sparse and merged).
+ */
+ match = path_matches_pattern_list(ct_path, ct_pathlen,
+ NULL, &dtype, pl, istate);
+ if (match != NOT_MATCHED)
+ can_convert = 0;
+
+ for (i = start; can_convert && i < end; i++) {
+ struct cache_entry *ce = istate->cache[i];
+
+ if (ce_stage(ce) ||
+ !(ce->ce_flags & CE_SKIP_WORKTREE))
+ can_convert = 0;
+ }
+
+ if (can_convert) {
+ struct cache_entry *se;
+ se = construct_sparse_dir_entry(istate, ct_path, ct);
+
+ istate->cache[num_converted++] = se;
+ return 1;
+ }
+
+ for (i = start; i < end; ) {
+ int count, span, pos = -1;
+ const char *base, *slash;
+ struct cache_entry *ce = istate->cache[i];
+
+ /*
+ * Detect if this is a normal entry outside of any subtree
+ * entry.
+ */
+ base = ce->name + ct_pathlen;
+ slash = strchr(base, '/');
+
+ if (slash)
+ pos = cache_tree_subtree_pos(ct, base, slash - base);
+
+ if (pos < 0) {
+ istate->cache[num_converted++] = ce;
+ i++;
+ continue;
+ }
+
+ strbuf_setlen(&child_path, 0);
+ strbuf_add(&child_path, ce->name, slash - ce->name + 1);
+
+ span = ct->down[pos]->cache_tree->entry_count;
+ count = convert_to_sparse_rec(istate,
+ num_converted, i, i + span,
+ child_path.buf, child_path.len,
+ ct->down[pos]->cache_tree);
+ num_converted += count;
+ i += span;
+ }
+
+ strbuf_release(&child_path);
+ return num_converted - start_converted;
+}
+
+int convert_to_sparse(struct index_state *istate)
+{
+ if (istate->split_index || istate->sparse_index ||
+ !core_apply_sparse_checkout || !core_sparse_checkout_cone)
+ return 0;
+
+ /*
+ * For now, only create a sparse index with the
+ * GIT_TEST_SPARSE_INDEX environment variable. We will relax
+ * this once we have a proper way to opt-in (and later still,
+ * opt-out).
+ */
+ if (!git_env_bool("GIT_TEST_SPARSE_INDEX", 0))
+ return 0;
+
+ if (!istate->sparse_checkout_patterns) {
+ istate->sparse_checkout_patterns = xcalloc(1, sizeof(struct pattern_list));
+ if (get_sparse_checkout_patterns(istate->sparse_checkout_patterns) < 0)
+ return 0;
+ }
+
+ if (!istate->sparse_checkout_patterns->use_cone_patterns) {
+ warning(_("attempting to use sparse-index without cone mode"));
+ return -1;
+ }
+
+ if (cache_tree_update(istate, 0)) {
+ warning(_("unable to update cache-tree, staying full"));
+ return -1;
+ }
+
+ remove_fsmonitor(istate);
+
+ trace2_region_enter("index", "convert_to_sparse", istate->repo);
+ istate->cache_nr = convert_to_sparse_rec(istate,
+ 0, 0, istate->cache_nr,
+ "", 0, istate->cache_tree);
+ istate->drop_cache_tree = 1;
+ istate->sparse_index = 1;
+ trace2_region_leave("index", "convert_to_sparse", istate->repo);
+ return 0;
+}
static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
{
diff --git a/sparse-index.h b/sparse-index.h
index 09a20d036c..64380e121d 100644
--- a/sparse-index.h
+++ b/sparse-index.h
@@ -3,5 +3,6 @@
struct index_state;
void ensure_full_index(struct index_state *istate);
+int convert_to_sparse(struct index_state *istate);
#endif
diff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh
index a1aea141c6..1e888d1951 100755
--- a/t/t1092-sparse-checkout-compatibility.sh
+++ b/t/t1092-sparse-checkout-compatibility.sh
@@ -2,6 +2,11 @@
test_description='compare full workdir to sparse workdir'
+# The verify_cache_tree() check is not sparse-aware (yet).
+# So, disable the check until that integration is complete.
+GIT_TEST_CHECK_CACHE_TREE=0
+GIT_TEST_SPLIT_INDEX=0
+
. ./test-lib.sh
test_expect_success 'setup' '
@@ -121,7 +126,9 @@ run_on_all () {
test_all_match () {
run_on_all "$@" &&
test_cmp full-checkout-out sparse-checkout-out &&
- test_cmp full-checkout-err sparse-checkout-err
+ test_cmp full-checkout-out sparse-index-out &&
+ test_cmp full-checkout-err sparse-checkout-err &&
+ test_cmp full-checkout-err sparse-index-err
}
test_sparse_match () {
@@ -130,6 +137,38 @@ test_sparse_match () {
test_cmp sparse-checkout-err sparse-index-err
}
+test_expect_success 'sparse-index contents' '
+ init_repos &&
+
+ test-tool -C sparse-index read-cache --table >cache &&
+ for dir in folder1 folder2 x
+ do
+ TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
+ grep "040000 tree $TREE $dir/" cache \
+ || return 1
+ done &&
+
+ GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout set folder1 &&
+
+ test-tool -C sparse-index read-cache --table >cache &&
+ for dir in deep folder2 x
+ do
+ TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
+ grep "040000 tree $TREE $dir/" cache \
+ || return 1
+ done &&
+
+ GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout set deep/deeper1 &&
+
+ test-tool -C sparse-index read-cache --table >cache &&
+ for dir in deep/deeper2 folder1 folder2 x
+ do
+ TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
+ grep "040000 tree $TREE $dir/" cache \
+ || return 1
+ done
+'
+
test_expect_success 'expanded in-memory index matches full index' '
init_repos &&
test_sparse_match test-tool read-cache --expand --table
@@ -137,6 +176,7 @@ test_expect_success 'expanded in-memory index matches full index' '
test_expect_success 'status with options' '
init_repos &&
+ test_sparse_match ls &&
test_all_match git status --porcelain=v2 &&
test_all_match git status --porcelain=v2 -z -u &&
test_all_match git status --porcelain=v2 -uno &&
@@ -273,6 +313,17 @@ test_expect_failure 'checkout and reset (mixed)' '
test_all_match git reset update-folder2
'
+# Ensure that sparse-index behaves identically to
+# sparse-checkout with a full index.
+test_expect_success 'checkout and reset (mixed) [sparse]' '
+ init_repos &&
+
+ test_sparse_match git checkout -b reset-test update-deep &&
+ test_sparse_match git reset deepest &&
+ test_sparse_match git reset update-folder1 &&
+ test_sparse_match git reset update-folder2
+'
+
test_expect_success 'merge' '
init_repos &&
@@ -309,14 +360,20 @@ test_expect_success 'clean' '
test_all_match git status --porcelain=v2 &&
test_all_match git clean -f &&
test_all_match git status --porcelain=v2 &&
+ test_sparse_match ls &&
+ test_sparse_match ls folder1 &&
test_all_match git clean -xf &&
test_all_match git status --porcelain=v2 &&
+ test_sparse_match ls &&
+ test_sparse_match ls folder1 &&
test_all_match git clean -xdf &&
test_all_match git status --porcelain=v2 &&
+ test_sparse_match ls &&
+ test_sparse_match ls folder1 &&
- test_path_is_dir sparse-checkout/folder1
+ test_sparse_match test_path_is_dir folder1
'
test_done