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>

Ævar Arnfjörð Bjarmason committed Jul 1, 2021 at 12:51 UTC ce93a4c6127abdf1ad9eacd537edd1c571a18e41
10 files changed +13 -27
builtin/add.c
+1 -2
@@ -470,7 +470,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
470 {
471 int exit_status = 0;
472 struct pathspec pathspec;
473 - struct dir_struct dir;
473 + struct dir_struct dir = DIR_INIT;
474 int flags;
475 int add_new_files;
476 int require_pathspec;
@@ -577,7 +577,6 @@ int cmd_add(int argc, const char **argv, const char *prefix)
577 die_in_unpopulated_submodule(&the_index, prefix);
578 die_path_inside_submodule(&the_index, &pathspec);
579
580 - dir_init(&dir);
580 if (add_new_files) {
581 int baselen;
582
builtin/check-ignore.c
+1 -2
@@ -153,7 +153,7 @@ static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
153 int cmd_check_ignore(int argc, const char **argv, const char *prefix)
154 {
155 int num_ignored;
156 - struct dir_struct dir;
156 + struct dir_struct dir = DIR_INIT;
157
158 git_config(git_default_config, NULL);
159
@@ -182,7 +182,6 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
182 if (!no_index && read_cache() < 0)
183 die(_("index file corrupt"));
184
185 - dir_init(&dir);
185 setup_standard_excludes(&dir);
186
187 if (stdin_paths) {
builtin/clean.c
+2 -4
@@ -641,7 +641,7 @@ static int clean_cmd(void)
641
642 static int filter_by_patterns_cmd(void)
643 {
644 - struct dir_struct dir;
644 + struct dir_struct dir = DIR_INIT;
645 struct strbuf confirm = STRBUF_INIT;
646 struct strbuf **ignore_list;
647 struct string_list_item *item;
@@ -665,7 +665,6 @@ static int filter_by_patterns_cmd(void)
665 if (!confirm.len)
666 break;
667
668 - dir_init(&dir);
668 pl = add_pattern_list(&dir, EXC_CMDL, "manual exclude");
669 ignore_list = strbuf_split_max(&confirm, ' ', 0);
670
@@ -890,7 +889,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
889 int ignored_only = 0, config_set = 0, errors = 0, gone = 1;
890 int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
891 struct strbuf abs_path = STRBUF_INIT;
893 - struct dir_struct dir;
892 + struct dir_struct dir = DIR_INIT;
893 struct pathspec pathspec;
894 struct strbuf buf = STRBUF_INIT;
895 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
@@ -921,7 +920,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
920 argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
921 0);
922
924 - dir_init(&dir);
923 if (!interactive && !dry_run && !force) {
924 if (config_set)
925 die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
builtin/grep.c
+1 -2
@@ -704,10 +704,9 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
704 static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
705 int exc_std, int use_index)
706 {
707 - struct dir_struct dir;
707 + struct dir_struct dir = DIR_INIT;
708 int i, hit = 0;
709
710 - dir_init(&dir);
710 if (!use_index)
711 dir.flags |= DIR_NO_GITLINKS;
712 if (exc_std)
builtin/ls-files.c
+1 -2
@@ -608,7 +608,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
608 {
609 int require_work_tree = 0, show_tag = 0, i;
610 char *max_prefix;
611 - struct dir_struct dir;
611 + struct dir_struct dir = DIR_INIT;
612 struct pattern_list *pl;
613 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
614 struct option builtin_ls_files_options[] = {
@@ -678,7 +678,6 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
678 if (argc == 2 && !strcmp(argv[1], "-h"))
679 usage_with_options(ls_files_usage, builtin_ls_files_options);
680
681 - dir_init(&dir);
681 prefix = cmd_prefix;
682 if (prefix)
683 prefix_len = strlen(prefix);
builtin/stash.c
+1 -2
@@ -991,9 +991,8 @@ static int get_untracked_files(const struct pathspec *ps, int include_untracked,
991 {
992 int i;
993 int found = 0;
994 - struct dir_struct dir;
994 + struct dir_struct dir = DIR_INIT;
995
996 - dir_init(&dir);
996 if (include_untracked != INCLUDE_ALL_FILES)
997 setup_standard_excludes(&dir);
998
dir.c
+2 -7
@@ -53,12 +53,6 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
53 int check_only, int stop_at_first_file, const struct pathspec *pathspec);
54 static int resolve_dtype(int dtype, struct index_state *istate,
55 const char *path, int len);
56 -
57 -void dir_init(struct dir_struct *dir)
58 -{
59 - memset(dir, 0, sizeof(*dir));
60 -}
61 -
56 struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp)
57 {
58 struct dirent *e;
@@ -3105,6 +3099,7 @@ void dir_clear(struct dir_struct *dir)
3099 struct exclude_list_group *group;
3100 struct pattern_list *pl;
3101 struct exclude_stack *stk;
3102 + struct dir_struct new = DIR_INIT;
3103
3104 for (i = EXC_CMDL; i <= EXC_FILE; i++) {
3105 group = &dir->exclude_list_group[i];
@@ -3132,7 +3127,7 @@ void dir_clear(struct dir_struct *dir)
3127 }
3128 strbuf_release(&dir->basebuf);
3129
3135 - dir_init(dir);
3130 + memcpy(dir, &new, sizeof(*dir));
3131 }
3132
3133 struct ondisk_untracked_cache {
dir.h
+2 -2
@@ -342,6 +342,8 @@ struct dir_struct {
342 unsigned visited_directories;
343 };
344
345 +#define DIR_INIT { 0 }
346 +
347 struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp);
348
349 /*Count the number of slashes for string s*/
@@ -367,8 +369,6 @@ int match_pathspec(struct index_state *istate,
369 int report_path_error(const char *ps_matched, const struct pathspec *pathspec);
370 int within_depth(const char *name, int namelen, int depth, int max_depth);
371
370 -void dir_init(struct dir_struct *dir);
371 -
372 int fill_directory(struct dir_struct *dir,
373 struct index_state *istate,
374 const struct pathspec *pathspec);
merge.c
+1 -2
@@ -53,7 +53,7 @@ int checkout_fast_forward(struct repository *r,
53 struct unpack_trees_options opts;
54 struct tree_desc t[MAX_UNPACK_TREES];
55 int i, nr_trees = 0;
56 - struct dir_struct dir;
56 + struct dir_struct dir = DIR_INIT;
57 struct lock_file lock_file = LOCK_INIT;
58
59 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
@@ -80,7 +80,6 @@ int checkout_fast_forward(struct repository *r,
80 }
81
82 memset(&opts, 0, sizeof(opts));
83 - dir_init(&dir);
83 if (overwrite_ignore) {
84 dir.flags |= DIR_SHOW_IGNORED;
85 setup_standard_excludes(&dir);
wt-status.c
+1 -2
@@ -699,14 +699,13 @@ static void wt_status_collect_changes_initial(struct wt_status *s)
699 static void wt_status_collect_untracked(struct wt_status *s)
700 {
701 int i;
702 - struct dir_struct dir;
702 + struct dir_struct dir = DIR_INIT;
703 uint64_t t_begin = getnanotime();
704 struct index_state *istate = s->repo->index;
705
706 if (!s->show_untracked_files)
707 return;
708
709 - dir_init(&dir);
709 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
710 dir.flags |=
711 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;