Skip to content

Commit 2e5932a

Browse files
committed
restore: avoid sparse index expansion
Teach update_some() to handle sparse directory entries at the tree level rather than expanding the entire sparse index. When iterating a source tree during checkout/restore operations: - If a directory matches a sparse directory entry with the same OID, skip it entirely (no change needed). - If the OID differs and we are in non-overlay mode (e.g., restore --staged), update the sparse directory entry's OID in place. This is semantically correct because non-overlay mode removes paths not in the source tree anyway. - In overlay mode (e.g., checkout <tree> -- .), fall through to recursive descent so individual file entries are preserved correctly. Also switch from index_name_pos() to index_name_pos_sparse() for individual file lookups to avoid triggering ensure_full_index() when the file is already individually tracked in the index. Update the test expectation in t1092 to assert that 'restore --staged' no longer expands the sparse index. Signed-off-by: Derrick Stolee <stolee@gmail.com>
1 parent 6bb9745 commit 2e5932a

2 files changed

Lines changed: 55 additions & 10 deletions

File tree

builtin/checkout.c

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "resolve-undo.h"
3232
#include "revision.h"
3333
#include "setup.h"
34+
#include "sparse-index.h"
3435
#include "submodule.h"
3536
#include "symlinks.h"
3637
#include "trace2.h"
@@ -133,14 +134,56 @@ static int post_checkout_hook(struct commit *old_commit, struct commit *new_comm
133134
}
134135

135136
static int update_some(const struct object_id *oid, struct strbuf *base,
136-
const char *pathname, unsigned mode, void *context UNUSED)
137+
const char *pathname, unsigned mode, void *context)
137138
{
138139
int len;
139140
struct cache_entry *ce;
140141
int pos;
142+
int overlay_mode = context ? *((int *)context) : 1;
141143

142-
if (S_ISDIR(mode))
144+
if (S_ISDIR(mode)) {
145+
/*
146+
* If this directory exists as a sparse directory entry in
147+
* the index, we can handle it at the tree level without
148+
* descending into individual files.
149+
*/
150+
if (the_repository->index->sparse_index) {
151+
struct strbuf dirpath = STRBUF_INIT;
152+
153+
strbuf_addbuf(&dirpath, base);
154+
strbuf_addstr(&dirpath, pathname);
155+
strbuf_addch(&dirpath, '/');
156+
157+
pos = index_name_pos_sparse(the_repository->index,
158+
dirpath.buf, dirpath.len);
159+
if (pos >= 0) {
160+
struct cache_entry *old =
161+
the_repository->index->cache[pos];
162+
if (S_ISSPARSEDIR(old->ce_mode)) {
163+
if (oideq(oid, &old->oid)) {
164+
strbuf_release(&dirpath);
165+
return 0;
166+
}
167+
if (!overlay_mode) {
168+
/*
169+
* In non-overlay mode (e.g.,
170+
* restore --staged), we can
171+
* replace the sparse dir OID
172+
* directly since files not in
173+
* the source tree should be
174+
* removed anyway.
175+
*/
176+
oidcpy(&old->oid, oid);
177+
old->ce_flags |= CE_UPDATE;
178+
strbuf_release(&dirpath);
179+
return 0;
180+
}
181+
}
182+
}
183+
strbuf_release(&dirpath);
184+
}
143185
return READ_TREE_RECURSIVE;
186+
}
144187

145188
len = base->len + strlen(pathname);
146189
ce = make_empty_cache_entry(the_repository->index, len);
@@ -156,7 +199,7 @@ static int update_some(const struct object_id *oid, struct strbuf *base,
156199
* entry in place. Whether it is UPTODATE or not, checkout_entry will
157200
* do the right thing.
158201
*/
159-
pos = index_name_pos(the_repository->index, ce->name, ce->ce_namelen);
202+
pos = index_name_pos_sparse(the_repository->index, ce->name, ce->ce_namelen);
160203
if (pos >= 0) {
161204
struct cache_entry *old = the_repository->index->cache[pos];
162205
if (ce->ce_mode == old->ce_mode &&
@@ -173,10 +216,11 @@ static int update_some(const struct object_id *oid, struct strbuf *base,
173216
return 0;
174217
}
175218

176-
static int read_tree_some(struct tree *tree, const struct pathspec *pathspec)
219+
static int read_tree_some(struct tree *tree, const struct pathspec *pathspec,
220+
int overlay_mode)
177221
{
178222
read_tree(the_repository, tree,
179-
pathspec, update_some, NULL);
223+
pathspec, update_some, &overlay_mode);
180224

181225
/* update the index with the given tree's info
182226
* for all args, expanding wildcards, and exit
@@ -571,7 +615,8 @@ static int checkout_paths(const struct checkout_opts *opts,
571615
return error(_("index file corrupt"));
572616

573617
if (opts->source_tree)
574-
read_tree_some(opts->source_tree, &opts->pathspec);
618+
read_tree_some(opts->source_tree, &opts->pathspec,
619+
opts->overlay_mode);
575620
if (opts->merge)
576621
unmerge_index(the_repository->index, &opts->pathspec, CE_MATCHED);
577622

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,19 +2608,19 @@ test_expect_success 'restore --staged with wildcards' '
26082608
test_all_match git diff --cached
26092609
'
26102610

2611-
test_expect_success 'sparse-index is expanded: restore --staged' '
2611+
test_expect_success 'sparse-index is not expanded: restore --staged' '
26122612
init_repos &&
26132613
26142614
git -C sparse-index checkout -b restore-staged-exp base &&
26152615
git -C sparse-index reset --soft update-folder1 &&
2616-
ensure_expanded restore --staged .
2616+
ensure_not_expanded restore --staged .
26172617
'
26182618

2619-
test_expect_success 'sparse-index is expanded: restore --source --staged' '
2619+
test_expect_success 'sparse-index is not expanded: restore --source --staged' '
26202620
init_repos &&
26212621
26222622
git -C sparse-index checkout -b restore-source-staged base &&
2623-
ensure_expanded restore --source update-folder1 --staged .
2623+
ensure_not_expanded restore --source update-folder1 --staged .
26242624
'
26252625

26262626
test_done

0 commit comments

Comments
 (0)