Skip to content

Commit ce93a4c

Browse files
avargitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5726a6b commit ce93a4c

File tree

10 files changed

+13
-27
lines changed

10 files changed

+13
-27
lines changed

builtin/add.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
470470
{
471471
int exit_status = 0;
472472
struct pathspec pathspec;
473-
struct dir_struct dir;
473+
struct dir_struct dir = DIR_INIT;
474474
int flags;
475475
int add_new_files;
476476
int require_pathspec;
@@ -577,7 +577,6 @@ int cmd_add(int argc, const char **argv, const char *prefix)
577577
die_in_unpopulated_submodule(&the_index, prefix);
578578
die_path_inside_submodule(&the_index, &pathspec);
579579

580-
dir_init(&dir);
581580
if (add_new_files) {
582581
int baselen;
583582

builtin/check-ignore.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
153153
int cmd_check_ignore(int argc, const char **argv, const char *prefix)
154154
{
155155
int num_ignored;
156-
struct dir_struct dir;
156+
struct dir_struct dir = DIR_INIT;
157157

158158
git_config(git_default_config, NULL);
159159

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

185-
dir_init(&dir);
186185
setup_standard_excludes(&dir);
187186

188187
if (stdin_paths) {

builtin/clean.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ static int clean_cmd(void)
641641

642642
static int filter_by_patterns_cmd(void)
643643
{
644-
struct dir_struct dir;
644+
struct dir_struct dir = DIR_INIT;
645645
struct strbuf confirm = STRBUF_INIT;
646646
struct strbuf **ignore_list;
647647
struct string_list_item *item;
@@ -665,7 +665,6 @@ static int filter_by_patterns_cmd(void)
665665
if (!confirm.len)
666666
break;
667667

668-
dir_init(&dir);
669668
pl = add_pattern_list(&dir, EXC_CMDL, "manual exclude");
670669
ignore_list = strbuf_split_max(&confirm, ' ', 0);
671670

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

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

builtin/grep.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,10 +704,9 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
704704
static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
705705
int exc_std, int use_index)
706706
{
707-
struct dir_struct dir;
707+
struct dir_struct dir = DIR_INIT;
708708
int i, hit = 0;
709709

710-
dir_init(&dir);
711710
if (!use_index)
712711
dir.flags |= DIR_NO_GITLINKS;
713712
if (exc_std)

builtin/ls-files.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
608608
{
609609
int require_work_tree = 0, show_tag = 0, i;
610610
char *max_prefix;
611-
struct dir_struct dir;
611+
struct dir_struct dir = DIR_INIT;
612612
struct pattern_list *pl;
613613
struct string_list exclude_list = STRING_LIST_INIT_NODUP;
614614
struct option builtin_ls_files_options[] = {
@@ -678,7 +678,6 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
678678
if (argc == 2 && !strcmp(argv[1], "-h"))
679679
usage_with_options(ls_files_usage, builtin_ls_files_options);
680680

681-
dir_init(&dir);
682681
prefix = cmd_prefix;
683682
if (prefix)
684683
prefix_len = strlen(prefix);

builtin/stash.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,9 +991,8 @@ static int get_untracked_files(const struct pathspec *ps, int include_untracked,
991991
{
992992
int i;
993993
int found = 0;
994-
struct dir_struct dir;
994+
struct dir_struct dir = DIR_INIT;
995995

996-
dir_init(&dir);
997996
if (include_untracked != INCLUDE_ALL_FILES)
998997
setup_standard_excludes(&dir);
999998

dir.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
5353
int check_only, int stop_at_first_file, const struct pathspec *pathspec);
5454
static int resolve_dtype(int dtype, struct index_state *istate,
5555
const char *path, int len);
56-
57-
void dir_init(struct dir_struct *dir)
58-
{
59-
memset(dir, 0, sizeof(*dir));
60-
}
61-
6256
struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp)
6357
{
6458
struct dirent *e;
@@ -3105,6 +3099,7 @@ void dir_clear(struct dir_struct *dir)
31053099
struct exclude_list_group *group;
31063100
struct pattern_list *pl;
31073101
struct exclude_stack *stk;
3102+
struct dir_struct new = DIR_INIT;
31083103

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

3135-
dir_init(dir);
3130+
memcpy(dir, &new, sizeof(*dir));
31363131
}
31373132

31383133
struct ondisk_untracked_cache {

dir.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ struct dir_struct {
342342
unsigned visited_directories;
343343
};
344344

345+
#define DIR_INIT { 0 }
346+
345347
struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp);
346348

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

370-
void dir_init(struct dir_struct *dir);
371-
372372
int fill_directory(struct dir_struct *dir,
373373
struct index_state *istate,
374374
const struct pathspec *pathspec);

merge.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int checkout_fast_forward(struct repository *r,
5353
struct unpack_trees_options opts;
5454
struct tree_desc t[MAX_UNPACK_TREES];
5555
int i, nr_trees = 0;
56-
struct dir_struct dir;
56+
struct dir_struct dir = DIR_INIT;
5757
struct lock_file lock_file = LOCK_INIT;
5858

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

8282
memset(&opts, 0, sizeof(opts));
83-
dir_init(&dir);
8483
if (overwrite_ignore) {
8584
dir.flags |= DIR_SHOW_IGNORED;
8685
setup_standard_excludes(&dir);

wt-status.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,14 +699,13 @@ static void wt_status_collect_changes_initial(struct wt_status *s)
699699
static void wt_status_collect_untracked(struct wt_status *s)
700700
{
701701
int i;
702-
struct dir_struct dir;
702+
struct dir_struct dir = DIR_INIT;
703703
uint64_t t_begin = getnanotime();
704704
struct index_state *istate = s->repo->index;
705705

706706
if (!s->show_untracked_files)
707707
return;
708708

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

0 commit comments

Comments
 (0)