Skip to content

Commit 58300f4

Browse files
derrickstoleegitster
authored andcommitted
sparse-index: add index.sparse config option
When enabled, this config option signals that index writes should attempt to use sparse-directory entries. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0938e6f commit 58300f4

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

Documentation/config/index.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ index.recordOffsetTable::
1414
Defaults to 'true' if index.threads has been explicitly enabled,
1515
'false' otherwise.
1616

17+
index.sparse::
18+
When enabled, write the index using sparse-directory entries. This
19+
has no effect unless `core.sparseCheckout` and
20+
`core.sparseCheckoutCone` are both enabled. Defaults to 'false'.
21+
1722
index.threads::
1823
Specifies the number of threads to spawn when loading the index.
1924
This is meant to reduce index load time on multiprocessor machines.

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,7 @@ struct repository_format {
10591059
int worktree_config;
10601060
int is_bare;
10611061
int hash_algo;
1062+
int sparse_index;
10621063
char *work_tree;
10631064
struct string_list unknown_extensions;
10641065
struct string_list v1_only_extensions;

repo-settings.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,11 @@ void prepare_repo_settings(struct repository *r)
8585
* removed.
8686
*/
8787
r->settings.command_requires_full_index = 1;
88+
89+
/*
90+
* Initialize this as off.
91+
*/
92+
r->settings.sparse_index = 0;
93+
if (!repo_config_get_bool(r, "index.sparse", &value) && value)
94+
r->settings.sparse_index = 1;
8895
}

repository.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ struct repo_settings {
4242

4343
int core_multi_pack_index;
4444

45-
unsigned command_requires_full_index:1;
45+
unsigned command_requires_full_index:1,
46+
sparse_index:1;
4647
};
4748

4849
struct repository {

sparse-index.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,43 @@ static int convert_to_sparse_rec(struct index_state *istate,
102102
return num_converted - start_converted;
103103
}
104104

105+
static int enable_sparse_index(struct repository *repo)
106+
{
107+
const char *config_path = repo_git_path(repo, "config.worktree");
108+
109+
git_config_set_in_file_gently(config_path,
110+
"index.sparse",
111+
"true");
112+
113+
prepare_repo_settings(repo);
114+
repo->settings.sparse_index = 1;
115+
return 0;
116+
}
117+
105118
int convert_to_sparse(struct index_state *istate)
106119
{
107120
if (istate->split_index || istate->sparse_index ||
108121
!core_apply_sparse_checkout || !core_sparse_checkout_cone)
109122
return 0;
110123

124+
if (!istate->repo)
125+
istate->repo = the_repository;
126+
127+
/*
128+
* The GIT_TEST_SPARSE_INDEX environment variable triggers the
129+
* index.sparse config variable to be on.
130+
*/
131+
if (git_env_bool("GIT_TEST_SPARSE_INDEX", 0)) {
132+
int err = enable_sparse_index(istate->repo);
133+
if (err < 0)
134+
return err;
135+
}
136+
111137
/*
112-
* For now, only create a sparse index with the
113-
* GIT_TEST_SPARSE_INDEX environment variable. We will relax
114-
* this once we have a proper way to opt-in (and later still,
115-
* opt-out).
138+
* Only convert to sparse if index.sparse is set.
116139
*/
117-
if (!git_env_bool("GIT_TEST_SPARSE_INDEX", 0))
140+
prepare_repo_settings(istate->repo);
141+
if (!istate->repo->settings.sparse_index)
118142
return 0;
119143

120144
if (!istate->sparse_checkout_patterns) {

0 commit comments

Comments
 (0)