dir.[ch]: replace dir_init() with DIR_INIT

Remove the dir_init() function and replace it with a DIR_INIT
macro. In many cases in the codebase we need to initialize things with
a function for good reasons, e.g. needing to call another function on
initialization. The "dir_init()" function was not one such case, and
could trivially be replaced with a more idiomatic macro initialization
pattern.

The only place where we made use of its use of memset() was in
dir_clear() itself, which resets the contents of an an existing struct
pointer. Let's use the new "memcpy() a 'blank' struct on the stack"
idiom to do that reset.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Ævar Arnfjörð Bjarmason 2021-07-01 12:51:27 +02:00 committed by Junio C Hamano
parent 5726a6b401
commit ce93a4c612
10 changed files with 13 additions and 27 deletions

View File

@ -470,7 +470,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
{ {
int exit_status = 0; int exit_status = 0;
struct pathspec pathspec; struct pathspec pathspec;
struct dir_struct dir; struct dir_struct dir = DIR_INIT;
int flags; int flags;
int add_new_files; int add_new_files;
int require_pathspec; int require_pathspec;
@ -577,7 +577,6 @@ int cmd_add(int argc, const char **argv, const char *prefix)
die_in_unpopulated_submodule(&the_index, prefix); die_in_unpopulated_submodule(&the_index, prefix);
die_path_inside_submodule(&the_index, &pathspec); die_path_inside_submodule(&the_index, &pathspec);


dir_init(&dir);
if (add_new_files) { if (add_new_files) {
int baselen; int baselen;



View File

@ -153,7 +153,7 @@ static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
int cmd_check_ignore(int argc, const char **argv, const char *prefix) int cmd_check_ignore(int argc, const char **argv, const char *prefix)
{ {
int num_ignored; int num_ignored;
struct dir_struct dir; struct dir_struct dir = DIR_INIT;


git_config(git_default_config, NULL); git_config(git_default_config, NULL);


@ -182,7 +182,6 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
if (!no_index && read_cache() < 0) if (!no_index && read_cache() < 0)
die(_("index file corrupt")); die(_("index file corrupt"));


dir_init(&dir);
setup_standard_excludes(&dir); setup_standard_excludes(&dir);


if (stdin_paths) { if (stdin_paths) {

View File

@ -641,7 +641,7 @@ static int clean_cmd(void)


static int filter_by_patterns_cmd(void) static int filter_by_patterns_cmd(void)
{ {
struct dir_struct dir; struct dir_struct dir = DIR_INIT;
struct strbuf confirm = STRBUF_INIT; struct strbuf confirm = STRBUF_INIT;
struct strbuf **ignore_list; struct strbuf **ignore_list;
struct string_list_item *item; struct string_list_item *item;
@ -665,7 +665,6 @@ static int filter_by_patterns_cmd(void)
if (!confirm.len) if (!confirm.len)
break; break;


dir_init(&dir);
pl = add_pattern_list(&dir, EXC_CMDL, "manual exclude"); pl = add_pattern_list(&dir, EXC_CMDL, "manual exclude");
ignore_list = strbuf_split_max(&confirm, ' ', 0); ignore_list = strbuf_split_max(&confirm, ' ', 0);


@ -890,7 +889,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
int ignored_only = 0, config_set = 0, errors = 0, gone = 1; int ignored_only = 0, config_set = 0, errors = 0, gone = 1;
int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT; int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
struct strbuf abs_path = STRBUF_INIT; struct strbuf abs_path = STRBUF_INIT;
struct dir_struct dir; struct dir_struct dir = DIR_INIT;
struct pathspec pathspec; struct pathspec pathspec;
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
struct string_list exclude_list = STRING_LIST_INIT_NODUP; struct string_list exclude_list = STRING_LIST_INIT_NODUP;
@ -921,7 +920,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
argc = parse_options(argc, argv, prefix, options, builtin_clean_usage, argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
0); 0);


dir_init(&dir);
if (!interactive && !dry_run && !force) { if (!interactive && !dry_run && !force) {
if (config_set) if (config_set)
die(_("clean.requireForce set to true and neither -i, -n, nor -f given; " die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "

View File

@ -704,10 +704,9 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec, static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
int exc_std, int use_index) int exc_std, int use_index)
{ {
struct dir_struct dir; struct dir_struct dir = DIR_INIT;
int i, hit = 0; int i, hit = 0;


dir_init(&dir);
if (!use_index) if (!use_index)
dir.flags |= DIR_NO_GITLINKS; dir.flags |= DIR_NO_GITLINKS;
if (exc_std) if (exc_std)

View File

@ -608,7 +608,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
{ {
int require_work_tree = 0, show_tag = 0, i; int require_work_tree = 0, show_tag = 0, i;
char *max_prefix; char *max_prefix;
struct dir_struct dir; struct dir_struct dir = DIR_INIT;
struct pattern_list *pl; struct pattern_list *pl;
struct string_list exclude_list = STRING_LIST_INIT_NODUP; struct string_list exclude_list = STRING_LIST_INIT_NODUP;
struct option builtin_ls_files_options[] = { struct option builtin_ls_files_options[] = {
@ -678,7 +678,6 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
if (argc == 2 && !strcmp(argv[1], "-h")) if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(ls_files_usage, builtin_ls_files_options); usage_with_options(ls_files_usage, builtin_ls_files_options);


dir_init(&dir);
prefix = cmd_prefix; prefix = cmd_prefix;
if (prefix) if (prefix)
prefix_len = strlen(prefix); prefix_len = strlen(prefix);

View File

@ -991,9 +991,8 @@ static int get_untracked_files(const struct pathspec *ps, int include_untracked,
{ {
int i; int i;
int found = 0; int found = 0;
struct dir_struct dir; struct dir_struct dir = DIR_INIT;


dir_init(&dir);
if (include_untracked != INCLUDE_ALL_FILES) if (include_untracked != INCLUDE_ALL_FILES)
setup_standard_excludes(&dir); setup_standard_excludes(&dir);



9
dir.c
View File

@ -53,12 +53,6 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
int check_only, int stop_at_first_file, const struct pathspec *pathspec); int check_only, int stop_at_first_file, const struct pathspec *pathspec);
static int resolve_dtype(int dtype, struct index_state *istate, static int resolve_dtype(int dtype, struct index_state *istate,
const char *path, int len); const char *path, int len);

void dir_init(struct dir_struct *dir)
{
memset(dir, 0, sizeof(*dir));
}

struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp) struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp)
{ {
struct dirent *e; struct dirent *e;
@ -3105,6 +3099,7 @@ void dir_clear(struct dir_struct *dir)
struct exclude_list_group *group; struct exclude_list_group *group;
struct pattern_list *pl; struct pattern_list *pl;
struct exclude_stack *stk; struct exclude_stack *stk;
struct dir_struct new = DIR_INIT;


for (i = EXC_CMDL; i <= EXC_FILE; i++) { for (i = EXC_CMDL; i <= EXC_FILE; i++) {
group = &dir->exclude_list_group[i]; group = &dir->exclude_list_group[i];
@ -3132,7 +3127,7 @@ void dir_clear(struct dir_struct *dir)
} }
strbuf_release(&dir->basebuf); strbuf_release(&dir->basebuf);


dir_init(dir); memcpy(dir, &new, sizeof(*dir));
} }


struct ondisk_untracked_cache { struct ondisk_untracked_cache {

4
dir.h
View File

@ -342,6 +342,8 @@ struct dir_struct {
unsigned visited_directories; unsigned visited_directories;
}; };


#define DIR_INIT { 0 }

struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp); struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp);


/*Count the number of slashes for string s*/ /*Count the number of slashes for string s*/
@ -367,8 +369,6 @@ int match_pathspec(struct index_state *istate,
int report_path_error(const char *ps_matched, const struct pathspec *pathspec); int report_path_error(const char *ps_matched, const struct pathspec *pathspec);
int within_depth(const char *name, int namelen, int depth, int max_depth); int within_depth(const char *name, int namelen, int depth, int max_depth);


void dir_init(struct dir_struct *dir);

int fill_directory(struct dir_struct *dir, int fill_directory(struct dir_struct *dir,
struct index_state *istate, struct index_state *istate,
const struct pathspec *pathspec); const struct pathspec *pathspec);

View File

@ -53,7 +53,7 @@ int checkout_fast_forward(struct repository *r,
struct unpack_trees_options opts; struct unpack_trees_options opts;
struct tree_desc t[MAX_UNPACK_TREES]; struct tree_desc t[MAX_UNPACK_TREES];
int i, nr_trees = 0; int i, nr_trees = 0;
struct dir_struct dir; struct dir_struct dir = DIR_INIT;
struct lock_file lock_file = LOCK_INIT; struct lock_file lock_file = LOCK_INIT;


refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL); refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
@ -80,7 +80,6 @@ int checkout_fast_forward(struct repository *r,
} }


memset(&opts, 0, sizeof(opts)); memset(&opts, 0, sizeof(opts));
dir_init(&dir);
if (overwrite_ignore) { if (overwrite_ignore) {
dir.flags |= DIR_SHOW_IGNORED; dir.flags |= DIR_SHOW_IGNORED;
setup_standard_excludes(&dir); setup_standard_excludes(&dir);

View File

@ -699,14 +699,13 @@ static void wt_status_collect_changes_initial(struct wt_status *s)
static void wt_status_collect_untracked(struct wt_status *s) static void wt_status_collect_untracked(struct wt_status *s)
{ {
int i; int i;
struct dir_struct dir; struct dir_struct dir = DIR_INIT;
uint64_t t_begin = getnanotime(); uint64_t t_begin = getnanotime();
struct index_state *istate = s->repo->index; struct index_state *istate = s->repo->index;


if (!s->show_untracked_files) if (!s->show_untracked_files)
return; return;


dir_init(&dir);
if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES) if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
dir.flags |= dir.flags |=
DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES; DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;