Code cleanup around struct_type_init() functions.

* ab/struct-init:
  string-list.h users: change to use *_{nodup,dup}()
  string-list.[ch]: add a string_list_init_{nodup,dup}()
  dir.[ch]: replace dir_init() with DIR_INIT
  *.c *_init(): define in terms of corresponding *_INIT macro
  *.h: move some *_INIT to designated initializers
This commit is contained in:
Junio C Hamano 2021-07-16 17:42:52 -07:00
Родитель 832a239b72 bc40dfb10a
Коммит bd4232fac3
29 изменённых файлов: 76 добавлений и 64 удалений

Просмотреть файл

@ -101,9 +101,9 @@ int init_apply_state(struct apply_state *state,
state->ws_error_action = warn_on_ws_error; state->ws_error_action = warn_on_ws_error;
state->ws_ignore_action = ignore_ws_none; state->ws_ignore_action = ignore_ws_none;
state->linenr = 1; state->linenr = 1;
string_list_init(&state->fn_table, 0); string_list_init_nodup(&state->fn_table);
string_list_init(&state->limit_by_name, 0); string_list_init_nodup(&state->limit_by_name);
string_list_init(&state->symlink_changes, 0); string_list_init_nodup(&state->symlink_changes);
strbuf_init(&state->root, 0); strbuf_init(&state->root, 0);
git_apply_config(); git_apply_config();

Просмотреть файл

@ -645,7 +645,7 @@ int write_archive(int argc, const char **argv, const char *prefix,
args.pretty_ctx = &ctx; args.pretty_ctx = &ctx;
args.repo = repo; args.repo = repo;
args.prefix = prefix; args.prefix = prefix;
string_list_init(&args.extra_files, 1); string_list_init_dup(&args.extra_files);
argc = parse_archive_args(argc, argv, &ar, &args, name_hint, remote); argc = parse_archive_args(argc, argv, &ar, &args, name_hint, remote);
if (!startup_info->have_repository) { if (!startup_info->have_repository) {
/* /*

Просмотреть файл

@ -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;

Просмотреть файл

@ -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) {

Просмотреть файл

@ -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; "

Просмотреть файл

@ -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)

Просмотреть файл

@ -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);

Просмотреть файл

@ -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);

Просмотреть файл

@ -2073,7 +2073,7 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
e = xmalloc(sizeof(*e)); e = xmalloc(sizeof(*e));
hashmap_entry_init(&e->ent, strhash(key)); hashmap_entry_init(&e->ent, strhash(key));
e->key = xstrdup(key); e->key = xstrdup(key);
string_list_init(&e->value_list, 1); string_list_init_dup(&e->value_list);
hashmap_add(&cs->config_hash, &e->ent); hashmap_add(&cs->config_hash, &e->ent);
} }
si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value)); si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value));

Просмотреть файл

@ -10,8 +10,8 @@
void credential_init(struct credential *c) void credential_init(struct credential *c)
{ {
memset(c, 0, sizeof(*c)); struct credential blank = CREDENTIAL_INIT;
c->helpers.strdup_strings = 1; memcpy(c, &blank, sizeof(*c));
} }
void credential_clear(struct credential *c) void credential_clear(struct credential *c)

Просмотреть файл

@ -128,7 +128,9 @@ struct credential {
char *path; char *path;
}; };
#define CREDENTIAL_INIT { STRING_LIST_INIT_DUP } #define CREDENTIAL_INIT { \
.helpers = STRING_LIST_INIT_DUP, \
}
/* Initialize a credential structure, setting all fields to empty. */ /* Initialize a credential structure, setting all fields to empty. */
void credential_init(struct credential *); void credential_init(struct credential *);

9
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); 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
Просмотреть файл

@ -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);

Просмотреть файл

@ -143,8 +143,8 @@ void enable_delayed_checkout(struct checkout *state)
if (!state->delayed_checkout) { if (!state->delayed_checkout) {
state->delayed_checkout = xmalloc(sizeof(*state->delayed_checkout)); state->delayed_checkout = xmalloc(sizeof(*state->delayed_checkout));
state->delayed_checkout->state = CE_CAN_DELAY; state->delayed_checkout->state = CE_CAN_DELAY;
string_list_init(&state->delayed_checkout->filters, 0); string_list_init_nodup(&state->delayed_checkout->filters);
string_list_init(&state->delayed_checkout->paths, 0); string_list_init_nodup(&state->delayed_checkout->paths);
} }
} }

Просмотреть файл

@ -3,10 +3,8 @@
void jw_init(struct json_writer *jw) void jw_init(struct json_writer *jw)
{ {
strbuf_init(&jw->json, 0); struct json_writer blank = JSON_WRITER_INIT;
strbuf_init(&jw->open_stack, 0); memcpy(jw, &blank, sizeof(*jw));;
jw->need_comma = 0;
jw->pretty = 0;
} }
void jw_release(struct json_writer *jw) void jw_release(struct json_writer *jw)

Просмотреть файл

@ -64,7 +64,10 @@ struct json_writer
unsigned int pretty:1; unsigned int pretty:1;
}; };
#define JSON_WRITER_INIT { STRBUF_INIT, STRBUF_INIT, 0, 0 } #define JSON_WRITER_INIT { \
.json = STRBUF_INIT, \
.open_stack = STRBUF_INIT, \
}
void jw_init(struct json_writer *jw); void jw_init(struct json_writer *jw);
void jw_release(struct json_writer *jw); void jw_release(struct json_writer *jw);

Просмотреть файл

@ -1840,7 +1840,7 @@ static void compute_collisions(struct strmap *collisions,
free(new_path); free(new_path);
} else { } else {
CALLOC_ARRAY(collision_info, 1); CALLOC_ARRAY(collision_info, 1);
string_list_init(&collision_info->source_files, 0); string_list_init_nodup(&collision_info->source_files);
strmap_put(collisions, new_path, collision_info); strmap_put(collisions, new_path, collision_info);
} }
string_list_insert(&collision_info->source_files, string_list_insert(&collision_info->source_files,
@ -4030,7 +4030,7 @@ static void merge_start(struct merge_options *opt, struct merge_result *result)
*/ */
strmap_init_with_options(&opt->priv->paths, NULL, 0); strmap_init_with_options(&opt->priv->paths, NULL, 0);
strmap_init_with_options(&opt->priv->conflicted, NULL, 0); strmap_init_with_options(&opt->priv->conflicted, NULL, 0);
string_list_init(&opt->priv->paths_to_free, 0); string_list_init_nodup(&opt->priv->paths_to_free);
/* /*
* keys & strbufs in output will sometimes need to outlive "paths", * keys & strbufs in output will sometimes need to outlive "paths",

Просмотреть файл

@ -121,7 +121,7 @@ static void dir_rename_entry_init(struct dir_rename_entry *entry,
entry->dir = directory; entry->dir = directory;
entry->non_unique_new_dir = 0; entry->non_unique_new_dir = 0;
strbuf_init(&entry->new_dir, 0); strbuf_init(&entry->new_dir, 0);
string_list_init(&entry->possible_new_dirs, 0); string_list_init_nodup(&entry->possible_new_dirs);
} }
struct collision_entry { struct collision_entry {
@ -3710,7 +3710,7 @@ static int merge_start(struct merge_options *opt, struct tree *head)
} }
CALLOC_ARRAY(opt->priv, 1); CALLOC_ARRAY(opt->priv, 1);
string_list_init(&opt->priv->df_conflict_file_set, 1); string_list_init_dup(&opt->priv->df_conflict_file_set);
return 0; return 0;
} }

Просмотреть файл

@ -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);

Просмотреть файл

@ -1425,7 +1425,7 @@ static int packed_transaction_prepare(struct ref_store *ref_store,
*/ */
CALLOC_ARRAY(data, 1); CALLOC_ARRAY(data, 1);
string_list_init(&data->updates, 0); string_list_init_nodup(&data->updates);
transaction->backend_data = data; transaction->backend_data = data;

Просмотреть файл

@ -11,9 +11,8 @@
void child_process_init(struct child_process *child) void child_process_init(struct child_process *child)
{ {
memset(child, 0, sizeof(*child)); struct child_process blank = CHILD_PROCESS_INIT;
strvec_init(&child->args); memcpy(child, &blank, sizeof(*child));
strvec_init(&child->env_array);
} }
void child_process_clear(struct child_process *child) void child_process_clear(struct child_process *child)

Просмотреть файл

@ -141,7 +141,10 @@ struct child_process {
void *clean_on_exit_handler_cbdata; void *clean_on_exit_handler_cbdata;
}; };
#define CHILD_PROCESS_INIT { NULL, STRVEC_INIT, STRVEC_INIT } #define CHILD_PROCESS_INIT { \
.args = STRVEC_INIT, \
.env_array = STRVEC_INIT, \
}
/** /**
* The functions: child_process_init, start_command, finish_command, * The functions: child_process_init, start_command, finish_command,

Просмотреть файл

@ -52,8 +52,8 @@ char strbuf_slopbuf[1];
void strbuf_init(struct strbuf *sb, size_t hint) void strbuf_init(struct strbuf *sb, size_t hint)
{ {
sb->alloc = sb->len = 0; struct strbuf blank = STRBUF_INIT;
sb->buf = strbuf_slopbuf; memcpy(sb, &blank, sizeof(*sb));
if (hint) if (hint)
strbuf_grow(sb, hint); strbuf_grow(sb, hint);
} }

Просмотреть файл

@ -1,10 +1,24 @@
#include "cache.h" #include "cache.h"
#include "string-list.h" #include "string-list.h"
void string_list_init_nodup(struct string_list *list)
{
struct string_list blank = STRING_LIST_INIT_NODUP;
memcpy(list, &blank, sizeof(*list));
}
void string_list_init_dup(struct string_list *list)
{
struct string_list blank = STRING_LIST_INIT_DUP;
memcpy(list, &blank, sizeof(*list));
}
void string_list_init(struct string_list *list, int strdup_strings) void string_list_init(struct string_list *list, int strdup_strings)
{ {
memset(list, 0, sizeof(*list)); if (strdup_strings)
list->strdup_strings = strdup_strings; string_list_init_dup(list);
else
string_list_init_nodup(list);
} }
/* if there is no exact match, point to the index where the entry could be /* if there is no exact match, point to the index where the entry could be

Просмотреть файл

@ -91,14 +91,21 @@ struct string_list {
compare_strings_fn cmp; /* NULL uses strcmp() */ compare_strings_fn cmp; /* NULL uses strcmp() */
}; };
#define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0, NULL } #define STRING_LIST_INIT_NODUP { 0 }
#define STRING_LIST_INIT_DUP { NULL, 0, 0, 1, NULL } #define STRING_LIST_INIT_DUP { .strdup_strings = 1 }
/* General functions which work with both sorted and unsorted lists. */ /* General functions which work with both sorted and unsorted lists. */
/** /**
* Initialize the members of the string_list, set `strdup_strings` * Initialize the members of a string_list pointer in the same way as
* member according to the value of the second parameter. * the corresponding `STRING_LIST_INIT_NODUP` and
* `STRING_LIST_INIT_DUP` macros.
*/
void string_list_init_nodup(struct string_list *list);
void string_list_init_dup(struct string_list *list);
/**
* TODO remove: For compatibility with any in-flight older API users
*/ */
void string_list_init(struct string_list *list, int strdup_strings); void string_list_init(struct string_list *list, int strdup_strings);

Просмотреть файл

@ -25,7 +25,8 @@ static struct strmap_entry *find_strmap_entry(struct strmap *map,
void strmap_init(struct strmap *map) void strmap_init(struct strmap *map)
{ {
strmap_init_with_options(map, NULL, 1); struct strmap blank = STRMAP_INIT;
memcpy(map, &blank, sizeof(*map));
} }
void strmap_init_with_options(struct strmap *map, void strmap_init_with_options(struct strmap *map,

Просмотреть файл

@ -6,9 +6,8 @@ const char *empty_strvec[] = { NULL };
void strvec_init(struct strvec *array) void strvec_init(struct strvec *array)
{ {
array->v = empty_strvec; struct strvec blank = STRVEC_INIT;
array->nr = 0; memcpy(array, &blank, sizeof(*array));
array->alloc = 0;
} }
static void strvec_push_nodup(struct strvec *array, const char *value) static void strvec_push_nodup(struct strvec *array, const char *value)

Просмотреть файл

@ -1055,7 +1055,7 @@ struct transport *transport_get(struct remote *remote, const char *url)
struct transport *ret = xcalloc(1, sizeof(*ret)); struct transport *ret = xcalloc(1, sizeof(*ret));
ret->progress = isatty(2); ret->progress = isatty(2);
string_list_init(&ret->pack_lockfiles, 1); string_list_init_dup(&ret->pack_lockfiles);
if (!remote) if (!remote)
BUG("No remote provided to transport_get()"); BUG("No remote provided to transport_get()");

Просмотреть файл

@ -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;