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 <stolee@gmail.com> Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jun 28, 2024 at 12:43 UTC b746a85d9a0d2384d3219b3b53593ccb880f3124
1 file changed +29 -16
sparse-index.c
+29 -16
@@ -439,8 +439,22 @@ void ensure_correct_sparsity(struct index_state *istate)
439 ensure_full_index(istate);
440 }
441
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)
458 {
459 struct stat st;
460 char *newdir;
@@ -450,7 +464,7 @@ static int path_found(const char *path, const char **dirname, size_t *dir_len,
464 * If dirname corresponds to a directory that doesn't exist, and this
465 * path starts with dirname, then path can't exist.
466 */
453 - if (!*dir_found && !memcmp(path, *dirname, *dir_len))
467 + if (!data->dir_found && !memcmp(path, data->dirname, data->dir_len))
468 return 0;
469
470 /*
@@ -472,15 +486,16 @@ static int path_found(const char *path, const char **dirname, size_t *dir_len,
486 * If path starts with directory (which we already lstat'ed and found),
487 * then no need to lstat parent directory again.
488 */
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))
491 return 0;
492
493 /* 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;
496
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);
499 free(tmp);
500
501 return 0;
@@ -488,9 +503,7 @@ static int path_found(const char *path, const char **dirname, size_t *dir_len,
503
504 static int clear_skip_worktree_from_present_files_sparse(struct index_state *istate)
505 {
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;
507
508 int path_count = 0;
509 int to_restart = 0;
@@ -502,7 +515,7 @@ static int clear_skip_worktree_from_present_files_sparse(struct index_state *ist
515
516 if (ce_skip_worktree(ce)) {
517 path_count++;
505 - if (path_found(ce->name, &last_dirname, &dir_len, &dir_found)) {
518 + if (path_found(ce->name, &data)) {
519 if (S_ISSPARSEDIR(ce->ce_mode)) {
520 to_restart = 1;
521 break;
@@ -516,14 +529,13 @@ static int clear_skip_worktree_from_present_files_sparse(struct index_state *ist
529 "sparse_path_count", path_count);
530 trace2_region_leave("index", "clear_skip_worktree_from_present_files_sparse",
531 istate->repo);
532 + clear_path_found_data(&data);
533 return to_restart;
534 }
535
536 static void clear_skip_worktree_from_present_files_full(struct index_state *istate)
537 {
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;
539
540 int path_count = 0;
541
@@ -537,7 +549,7 @@ static void clear_skip_worktree_from_present_files_full(struct index_state *ista
549
550 if (ce_skip_worktree(ce)) {
551 path_count++;
540 - if (path_found(ce->name, &last_dirname, &dir_len, &dir_found))
552 + if (path_found(ce->name, &data))
553 ce->ce_flags &= ~CE_SKIP_WORKTREE;
554 }
555 }
@@ -546,6 +558,7 @@ static void clear_skip_worktree_from_present_files_full(struct index_state *ista
558 "full_path_count", path_count);
559 trace2_region_leave("index", "clear_skip_worktree_from_present_files_full",
560 istate->repo);
561 + clear_path_found_data(&data);
562 }
563
564 void clear_skip_worktree_from_present_files(struct index_state *istate)