Skip to content

Commit 347410c

Browse files
committed
sparse-checkout: create helper methods
As we integrate the sparse index into more builtins, we occasionally need to check the sparse-checkout patterns to see if a path is within the sparse-checkout cone. Create some helper methods that help initialize the patterns and check for pattern matching to make this easier. The existing callers of commands like get_sparse_checkout_patterns() use a custom 'struct pattern_list' that is not necessarily the one in the 'struct index_state', so there are not many previous uses that could adopt these helpers. There are just two in builtin/add.c and sparse-index.c that can use path_in_sparse_checkout(). We add a path_in_cone_mode_sparse_checkout() as well that will only return false if the path is outside of the sparse-checkout definition _and_ the sparse-checkout patterns are in cone mode. Signed-off-by: Derrick Stolee <[email protected]>
1 parent e7cdaa0 commit 347410c

File tree

4 files changed

+66
-15
lines changed

4 files changed

+66
-15
lines changed

builtin/add.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,21 +190,16 @@ static int refresh(int verbose, const struct pathspec *pathspec)
190190
struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
191191
int flags = REFRESH_IGNORE_SKIP_WORKTREE |
192192
(verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET);
193-
struct pattern_list pl = { 0 };
194-
int sparse_checkout_enabled = !get_sparse_checkout_patterns(&pl);
195193

196194
seen = xcalloc(pathspec->nr, 1);
197195
refresh_index(&the_index, flags, pathspec, seen,
198196
_("Unstaged changes after refreshing the index:"));
199197
for (i = 0; i < pathspec->nr; i++) {
200198
if (!seen[i]) {
201199
const char *path = pathspec->items[i].original;
202-
int dtype = DT_REG;
203200

204201
if (matches_skip_worktree(pathspec, i, &skip_worktree_seen) ||
205-
(sparse_checkout_enabled &&
206-
!path_matches_pattern_list(path, strlen(path), NULL,
207-
&dtype, &pl, &the_index))) {
202+
!path_in_sparse_checkout(path, &the_index)) {
208203
string_list_append(&only_match_skip_worktree,
209204
pathspec->items[i].original);
210205
} else {

dir.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,60 @@ enum pattern_match_result path_matches_pattern_list(
14391439
return result;
14401440
}
14411441

1442+
int init_sparse_checkout_patterns(struct index_state *istate)
1443+
{
1444+
if (!core_apply_sparse_checkout ||
1445+
istate->sparse_checkout_patterns)
1446+
return 0;
1447+
1448+
CALLOC_ARRAY(istate->sparse_checkout_patterns, 1);
1449+
1450+
if (get_sparse_checkout_patterns(istate->sparse_checkout_patterns) < 0) {
1451+
FREE_AND_NULL(istate->sparse_checkout_patterns);
1452+
return -1;
1453+
}
1454+
1455+
return 0;
1456+
}
1457+
1458+
static int path_in_sparse_checkout_1(const char *path,
1459+
struct index_state *istate,
1460+
int require_cone_mode)
1461+
{
1462+
const char *base;
1463+
int dtype = DT_REG;
1464+
init_sparse_checkout_patterns(istate);
1465+
1466+
/*
1467+
* We default to accepting a path if there are no patterns or
1468+
* they are of the wrong type.
1469+
*/
1470+
if (!istate->sparse_checkout_patterns ||
1471+
(require_cone_mode &&
1472+
!istate->sparse_checkout_patterns->use_cone_patterns))
1473+
return 1;
1474+
1475+
1476+
1477+
base = strrchr(path, '/');
1478+
return path_matches_pattern_list(path, strlen(path), base ? base + 1 : path,
1479+
&dtype,
1480+
istate->sparse_checkout_patterns,
1481+
istate) > 0;
1482+
}
1483+
1484+
int path_in_sparse_checkout(const char *path,
1485+
struct index_state *istate)
1486+
{
1487+
return path_in_sparse_checkout_1(path, istate, 0);
1488+
}
1489+
1490+
int path_in_cone_modesparse_checkout(const char *path,
1491+
struct index_state *istate)
1492+
{
1493+
return path_in_sparse_checkout_1(path, istate, 1);
1494+
}
1495+
14421496
static struct path_pattern *last_matching_pattern_from_lists(
14431497
struct dir_struct *dir, struct index_state *istate,
14441498
const char *pathname, int pathlen,

dir.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,14 @@ enum pattern_match_result path_matches_pattern_list(const char *pathname,
394394
const char *basename, int *dtype,
395395
struct pattern_list *pl,
396396
struct index_state *istate);
397+
398+
int init_sparse_checkout_patterns(struct index_state *state);
399+
400+
int path_in_sparse_checkout(const char *path,
401+
struct index_state *istate);
402+
int path_in_cone_modesparse_checkout(const char *path,
403+
struct index_state *istate);
404+
397405
struct dir_entry *dir_add_ignored(struct dir_struct *dir,
398406
struct index_state *istate,
399407
const char *pathname, int len);

sparse-index.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,14 @@ static int convert_to_sparse_rec(struct index_state *istate,
3434
int i, can_convert = 1;
3535
int start_converted = num_converted;
3636
enum pattern_match_result match;
37-
int dtype = DT_UNKNOWN;
3837
struct strbuf child_path = STRBUF_INIT;
39-
struct pattern_list *pl = istate->sparse_checkout_patterns;
4038

4139
/*
4240
* Is the current path outside of the sparse cone?
4341
* Then check if the region can be replaced by a sparse
4442
* directory entry (everything is sparse and merged).
4543
*/
46-
match = path_matches_pattern_list(ct_path, ct_pathlen,
47-
NULL, &dtype, pl, istate);
44+
match = path_in_sparse_checkout(ct_path, istate);
4845
if (match != NOT_MATCHED)
4946
can_convert = 0;
5047

@@ -153,11 +150,8 @@ int convert_to_sparse(struct index_state *istate)
153150
if (!istate->repo->settings.sparse_index)
154151
return 0;
155152

156-
if (!istate->sparse_checkout_patterns) {
157-
istate->sparse_checkout_patterns = xcalloc(1, sizeof(struct pattern_list));
158-
if (get_sparse_checkout_patterns(istate->sparse_checkout_patterns) < 0)
159-
return 0;
160-
}
153+
if (init_sparse_checkout_patterns(istate) < 0)
154+
return 0;
161155

162156
/*
163157
* We need cone-mode patterns to use sparse-index. If a user edits

0 commit comments

Comments
 (0)