Skip to content

Commit b746a85

Browse files
derrickstoleegitster
authored andcommitted
sparse-index: refactor path_found()
In advance of changing the behavior of path_found(), take all of the intermediate data values and group them into a single struct. This simplifies the method prototype as well as the initialization. Future changes can be made directly to the struct and method without changing the callers with this approach. Note that the clear_path_found_data() method is currently empty, as there is nothing to free. This method is a placeholder for future changes that require a non-trivial implementation. Its stub is created now so consumers could call it now and not change in future changes. Signed-off-by: Derrick Stolee <[email protected]> Reviewed-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 532e216 commit b746a85

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

sparse-index.c

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,22 @@ void ensure_correct_sparsity(struct index_state *istate)
439439
ensure_full_index(istate);
440440
}
441441

442-
static int path_found(const char *path, const char **dirname, size_t *dir_len,
443-
int *dir_found)
442+
struct path_found_data {
443+
const char *dirname;
444+
size_t dir_len;
445+
int dir_found;
446+
};
447+
448+
#define PATH_FOUND_DATA_INIT { \
449+
.dir_found = 1 \
450+
}
451+
452+
static void clear_path_found_data(struct path_found_data *data)
453+
{
454+
return;
455+
}
456+
457+
static int path_found(const char *path, struct path_found_data *data)
444458
{
445459
struct stat st;
446460
char *newdir;
@@ -450,7 +464,7 @@ static int path_found(const char *path, const char **dirname, size_t *dir_len,
450464
* If dirname corresponds to a directory that doesn't exist, and this
451465
* path starts with dirname, then path can't exist.
452466
*/
453-
if (!*dir_found && !memcmp(path, *dirname, *dir_len))
467+
if (!data->dir_found && !memcmp(path, data->dirname, data->dir_len))
454468
return 0;
455469

456470
/*
@@ -472,25 +486,24 @@ static int path_found(const char *path, const char **dirname, size_t *dir_len,
472486
* If path starts with directory (which we already lstat'ed and found),
473487
* then no need to lstat parent directory again.
474488
*/
475-
if (*dir_found && *dirname && memcmp(path, *dirname, *dir_len))
489+
if (data->dir_found && data->dirname &&
490+
memcmp(path, data->dirname, data->dir_len))
476491
return 0;
477492

478493
/* Free previous dirname, and cache path's dirname */
479-
*dirname = path;
480-
*dir_len = newdir - path + 1;
494+
data->dirname = path;
495+
data->dir_len = newdir - path + 1;
481496

482-
tmp = xstrndup(path, *dir_len);
483-
*dir_found = !lstat(tmp, &st);
497+
tmp = xstrndup(path, data->dir_len);
498+
data->dir_found = !lstat(tmp, &st);
484499
free(tmp);
485500

486501
return 0;
487502
}
488503

489504
static int clear_skip_worktree_from_present_files_sparse(struct index_state *istate)
490505
{
491-
const char *last_dirname = NULL;
492-
size_t dir_len = 0;
493-
int dir_found = 1;
506+
struct path_found_data data = PATH_FOUND_DATA_INIT;
494507

495508
int path_count = 0;
496509
int to_restart = 0;
@@ -502,7 +515,7 @@ static int clear_skip_worktree_from_present_files_sparse(struct index_state *ist
502515

503516
if (ce_skip_worktree(ce)) {
504517
path_count++;
505-
if (path_found(ce->name, &last_dirname, &dir_len, &dir_found)) {
518+
if (path_found(ce->name, &data)) {
506519
if (S_ISSPARSEDIR(ce->ce_mode)) {
507520
to_restart = 1;
508521
break;
@@ -516,14 +529,13 @@ static int clear_skip_worktree_from_present_files_sparse(struct index_state *ist
516529
"sparse_path_count", path_count);
517530
trace2_region_leave("index", "clear_skip_worktree_from_present_files_sparse",
518531
istate->repo);
532+
clear_path_found_data(&data);
519533
return to_restart;
520534
}
521535

522536
static void clear_skip_worktree_from_present_files_full(struct index_state *istate)
523537
{
524-
const char *last_dirname = NULL;
525-
size_t dir_len = 0;
526-
int dir_found = 1;
538+
struct path_found_data data = PATH_FOUND_DATA_INIT;
527539

528540
int path_count = 0;
529541

@@ -537,7 +549,7 @@ static void clear_skip_worktree_from_present_files_full(struct index_state *ista
537549

538550
if (ce_skip_worktree(ce)) {
539551
path_count++;
540-
if (path_found(ce->name, &last_dirname, &dir_len, &dir_found))
552+
if (path_found(ce->name, &data))
541553
ce->ce_flags &= ~CE_SKIP_WORKTREE;
542554
}
543555
}
@@ -546,6 +558,7 @@ static void clear_skip_worktree_from_present_files_full(struct index_state *ista
546558
"full_path_count", path_count);
547559
trace2_region_leave("index", "clear_skip_worktree_from_present_files_full",
548560
istate->repo);
561+
clear_path_found_data(&data);
549562
}
550563

551564
void clear_skip_worktree_from_present_files(struct index_state *istate)

0 commit comments

Comments
 (0)