]> git.ipfire.org Git - thirdparty/git.git/commitdiff
dir.[ch]: replace dir_init() with DIR_INIT
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Thu, 1 Jul 2021 10:51:27 +0000 (12:51 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 1 Jul 2021 19:32:22 +0000 (12:32 -0700)
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>
builtin/add.c
builtin/check-ignore.c
builtin/clean.c
builtin/grep.c
builtin/ls-files.c
builtin/stash.c
dir.c
dir.h
merge.c
wt-status.c

index b773b5a4993e3e0e955199d07a23669f051e0be9..09e684585d9ff25e708434000e09f807b1d30a45 100644 (file)
@@ -470,7 +470,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 {
        int exit_status = 0;
        struct pathspec pathspec;
-       struct dir_struct dir;
+       struct dir_struct dir = DIR_INIT;
        int flags;
        int add_new_files;
        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_path_inside_submodule(&the_index, &pathspec);
 
-       dir_init(&dir);
        if (add_new_files) {
                int baselen;
 
index 81234552b7f639496e2452be23c8ae8374294645..21912569650d11c07297193a24e041bce035a284 100644 (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 num_ignored;
-       struct dir_struct dir;
+       struct dir_struct dir = DIR_INIT;
 
        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)
                die(_("index file corrupt"));
 
-       dir_init(&dir);
        setup_standard_excludes(&dir);
 
        if (stdin_paths) {
index 4944cf440b477b073a05165c0a41c2ff4d26a827..98a2860409bb4820af393ea9e50e58124fd7873e 100644 (file)
@@ -641,7 +641,7 @@ static int clean_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 **ignore_list;
        struct string_list_item *item;
@@ -665,7 +665,6 @@ static int filter_by_patterns_cmd(void)
                if (!confirm.len)
                        break;
 
-               dir_init(&dir);
                pl = add_pattern_list(&dir, EXC_CMDL, "manual exclude");
                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 rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
        struct strbuf abs_path = STRBUF_INIT;
-       struct dir_struct dir;
+       struct dir_struct dir = DIR_INIT;
        struct pathspec pathspec;
        struct strbuf buf = STRBUF_INIT;
        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,
                             0);
 
-       dir_init(&dir);
        if (!interactive && !dry_run && !force) {
                if (config_set)
                        die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
index ab8822e68f42dab702d096f28b88c492e6a3f4ef..7d2f8e5adb69c428847bc23b81bf822c916ccdea 100644 (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,
                          int exc_std, int use_index)
 {
-       struct dir_struct dir;
+       struct dir_struct dir = DIR_INIT;
        int i, hit = 0;
 
-       dir_init(&dir);
        if (!use_index)
                dir.flags |= DIR_NO_GITLINKS;
        if (exc_std)
index 45cc3b23dd6845d902e64997a80c7c7347110c6d..29a26ad8ae483b6055268ca2a238b8b7e9c09b74 100644 (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;
        char *max_prefix;
-       struct dir_struct dir;
+       struct dir_struct dir = DIR_INIT;
        struct pattern_list *pl;
        struct string_list exclude_list = STRING_LIST_INIT_NODUP;
        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"))
                usage_with_options(ls_files_usage, builtin_ls_files_options);
 
-       dir_init(&dir);
        prefix = cmd_prefix;
        if (prefix)
                prefix_len = strlen(prefix);
index 01066d708548ab6ca10964e1289aefa25e0aa61f..dc50a4b292cc62ef0fc94b7e55b3a591f34259c0 100644 (file)
@@ -991,9 +991,8 @@ static int get_untracked_files(const struct pathspec *ps, int include_untracked,
 {
        int i;
        int found = 0;
-       struct dir_struct dir;
+       struct dir_struct dir = DIR_INIT;
 
-       dir_init(&dir);
        if (include_untracked != INCLUDE_ALL_FILES)
                setup_standard_excludes(&dir);
 
diff --git a/dir.c b/dir.c
index ebe5ec046e050683443507495b3dcfa903c420e4..313e9324597a1ae1b03b3979be412b86ab63dcd0 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -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);
 static int resolve_dtype(int dtype, struct index_state *istate,
                         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 *e;
@@ -3105,6 +3099,7 @@ void dir_clear(struct dir_struct *dir)
        struct exclude_list_group *group;
        struct pattern_list *pl;
        struct exclude_stack *stk;
+       struct dir_struct new = DIR_INIT;
 
        for (i = EXC_CMDL; i <= EXC_FILE; i++) {
                group = &dir->exclude_list_group[i];
@@ -3132,7 +3127,7 @@ void dir_clear(struct dir_struct *dir)
        }
        strbuf_release(&dir->basebuf);
 
-       dir_init(dir);
+       memcpy(dir, &new, sizeof(*dir));
 }
 
 struct ondisk_untracked_cache {
diff --git a/dir.h b/dir.h
index e3db9b9ec658eef53b62cd8a1bd83a5a64389bac..8d0ddd8f18d9c3f2ac0385d6acb1688b28e66432 100644 (file)
--- a/dir.h
+++ b/dir.h
@@ -342,6 +342,8 @@ struct dir_struct {
        unsigned visited_directories;
 };
 
+#define DIR_INIT { 0 }
+
 struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp);
 
 /*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 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,
                   struct index_state *istate,
                   const struct pathspec *pathspec);
diff --git a/merge.c b/merge.c
index 5fb88af10254a75557697345fd25cfcf19df9748..6e736881d900bf76f33c1684e411b498e37582bd 100644 (file)
--- a/merge.c
+++ b/merge.c
@@ -53,7 +53,7 @@ int checkout_fast_forward(struct repository *r,
        struct unpack_trees_options opts;
        struct tree_desc t[MAX_UNPACK_TREES];
        int i, nr_trees = 0;
-       struct dir_struct dir;
+       struct dir_struct dir = DIR_INIT;
        struct lock_file lock_file = LOCK_INIT;
 
        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));
-       dir_init(&dir);
        if (overwrite_ignore) {
                dir.flags |= DIR_SHOW_IGNORED;
                setup_standard_excludes(&dir);
index 42b673571696bd398fea34b969853975de2a010b..b5a3e1cc252a4b0c4a2a5843e6c1fe25adfc7523 100644 (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)
 {
        int i;
-       struct dir_struct dir;
+       struct dir_struct dir = DIR_INIT;
        uint64_t t_begin = getnanotime();
        struct index_state *istate = s->repo->index;
 
        if (!s->show_untracked_files)
                return;
 
-       dir_init(&dir);
        if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
                dir.flags |=
                        DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;