Skip to content

Commit 0d9f282

Browse files
hvoigtgitster
authored andcommitted
submodule: extract functions for config set and lookup
This is one step towards using the new configuration API. We just extract these functions to make replacing the actual code easier. Signed-off-by: Heiko Voigt <[email protected]> Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 959b545 commit 0d9f282

File tree

1 file changed

+97
-45
lines changed

1 file changed

+97
-45
lines changed

submodule.c

Lines changed: 97 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,76 @@ static int gitmodules_is_unmerged;
4141
*/
4242
static int gitmodules_is_modified;
4343

44+
static const char *get_name_for_path(const char *path)
45+
{
46+
struct string_list_item *path_option;
47+
if (path == NULL) {
48+
if (config_name_for_path.nr > 0)
49+
return config_name_for_path.items[0].util;
50+
else
51+
return NULL;
52+
}
53+
path_option = unsorted_string_list_lookup(&config_name_for_path, path);
54+
if (!path_option)
55+
return NULL;
56+
return path_option->util;
57+
}
58+
59+
static void set_name_for_path(const char *path, const char *name, int namelen)
60+
{
61+
struct string_list_item *config;
62+
config = unsorted_string_list_lookup(&config_name_for_path, path);
63+
if (config)
64+
free(config->util);
65+
else
66+
config = string_list_append(&config_name_for_path, xstrdup(path));
67+
config->util = xmemdupz(name, namelen);
68+
}
69+
70+
static const char *get_ignore_for_name(const char *name)
71+
{
72+
struct string_list_item *ignore_option;
73+
ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, name);
74+
if (!ignore_option)
75+
return NULL;
76+
77+
return ignore_option->util;
78+
}
79+
80+
static void set_ignore_for_name(const char *name, int namelen, const char *ignore)
81+
{
82+
struct string_list_item *config;
83+
char *name_cstr = xmemdupz(name, namelen);
84+
config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);
85+
if (config) {
86+
free(config->util);
87+
free(name_cstr);
88+
} else
89+
config = string_list_append(&config_ignore_for_name, name_cstr);
90+
config->util = xstrdup(ignore);
91+
}
92+
93+
static int get_fetch_recurse_for_name(const char *name)
94+
{
95+
struct string_list_item *fetch_recurse;
96+
fetch_recurse = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
97+
if (!fetch_recurse)
98+
return RECURSE_SUBMODULES_NONE;
99+
100+
return (intptr_t) fetch_recurse->util;
101+
}
102+
103+
static void set_fetch_recurse_for_name(const char *name, int namelen, int fetch_recurse)
104+
{
105+
struct string_list_item *config;
106+
char *name_cstr = xmemdupz(name, namelen);
107+
config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);
108+
if (!config)
109+
config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);
110+
else
111+
free(name_cstr);
112+
config->util = (void *)(intptr_t) fetch_recurse;
113+
}
44114

45115
int is_staging_gitmodules_ok(void)
46116
{
@@ -55,21 +125,21 @@ int is_staging_gitmodules_ok(void)
55125
int update_path_in_gitmodules(const char *oldpath, const char *newpath)
56126
{
57127
struct strbuf entry = STRBUF_INIT;
58-
struct string_list_item *path_option;
128+
const char *path;
59129

60130
if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
61131
return -1;
62132

63133
if (gitmodules_is_unmerged)
64134
die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
65135

66-
path_option = unsorted_string_list_lookup(&config_name_for_path, oldpath);
67-
if (!path_option) {
136+
path = get_name_for_path(oldpath);
137+
if (!path) {
68138
warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
69139
return -1;
70140
}
71141
strbuf_addstr(&entry, "submodule.");
72-
strbuf_addstr(&entry, path_option->util);
142+
strbuf_addstr(&entry, path);
73143
strbuf_addstr(&entry, ".path");
74144
if (git_config_set_in_file(".gitmodules", entry.buf, newpath) < 0) {
75145
/* Maybe the user already did that, don't error out here */
@@ -89,21 +159,21 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)
89159
int remove_path_from_gitmodules(const char *path)
90160
{
91161
struct strbuf sect = STRBUF_INIT;
92-
struct string_list_item *path_option;
162+
const char *path_option;
93163

94164
if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
95165
return -1;
96166

97167
if (gitmodules_is_unmerged)
98168
die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
99169

100-
path_option = unsorted_string_list_lookup(&config_name_for_path, path);
170+
path_option = get_name_for_path(path);
101171
if (!path_option) {
102172
warning(_("Could not find section in .gitmodules where path=%s"), path);
103173
return -1;
104174
}
105175
strbuf_addstr(&sect, "submodule.");
106-
strbuf_addstr(&sect, path_option->util);
176+
strbuf_addstr(&sect, path_option);
107177
if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) {
108178
/* Maybe the user already did that, don't error out here */
109179
warning(_("Could not remove .gitmodules entry for %s"), path);
@@ -165,12 +235,11 @@ static int add_submodule_odb(const char *path)
165235
void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
166236
const char *path)
167237
{
168-
struct string_list_item *path_option, *ignore_option;
169-
path_option = unsorted_string_list_lookup(&config_name_for_path, path);
170-
if (path_option) {
171-
ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
172-
if (ignore_option)
173-
handle_ignore_submodules_arg(diffopt, ignore_option->util);
238+
const char *name = get_name_for_path(path);
239+
if (name) {
240+
const char *ignore = get_ignore_for_name(name);
241+
if (ignore)
242+
handle_ignore_submodules_arg(diffopt, ignore);
174243
else if (gitmodules_is_unmerged)
175244
DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
176245
}
@@ -221,7 +290,6 @@ void gitmodules_config(void)
221290

222291
int parse_submodule_config_option(const char *var, const char *value)
223292
{
224-
struct string_list_item *config;
225293
const char *name, *key;
226294
int namelen;
227295

@@ -232,22 +300,14 @@ int parse_submodule_config_option(const char *var, const char *value)
232300
if (!value)
233301
return config_error_nonbool(var);
234302

235-
config = unsorted_string_list_lookup(&config_name_for_path, value);
236-
if (config)
237-
free(config->util);
238-
else
239-
config = string_list_append(&config_name_for_path, xstrdup(value));
240-
config->util = xmemdupz(name, namelen);
303+
set_name_for_path(value, name, namelen);
304+
241305
} else if (!strcmp(key, "fetchrecursesubmodules")) {
242-
char *name_cstr = xmemdupz(name, namelen);
243-
config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);
244-
if (!config)
245-
config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);
246-
else
247-
free(name_cstr);
248-
config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
306+
int fetch_recurse = parse_fetch_recurse_submodules_arg(var, value);
307+
308+
set_fetch_recurse_for_name(name, namelen, fetch_recurse);
309+
249310
} else if (!strcmp(key, "ignore")) {
250-
char *name_cstr;
251311

252312
if (!value)
253313
return config_error_nonbool(var);
@@ -258,14 +318,7 @@ int parse_submodule_config_option(const char *var, const char *value)
258318
return 0;
259319
}
260320

261-
name_cstr = xmemdupz(name, namelen);
262-
config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);
263-
if (config) {
264-
free(config->util);
265-
free(name_cstr);
266-
} else
267-
config = string_list_append(&config_ignore_for_name, name_cstr);
268-
config->util = xstrdup(value);
321+
set_ignore_for_name(name, namelen, value);
269322
return 0;
270323
}
271324
return 0;
@@ -646,7 +699,7 @@ static void calculate_changed_submodule_paths(void)
646699
struct argv_array argv = ARGV_ARRAY_INIT;
647700

648701
/* No need to check if there are no submodules configured */
649-
if (!config_name_for_path.nr)
702+
if (!get_name_for_path(NULL))
650703
return;
651704

652705
init_revisions(&rev, NULL);
@@ -693,7 +746,7 @@ int fetch_populated_submodules(const struct argv_array *options,
693746
int i, result = 0;
694747
struct child_process cp = CHILD_PROCESS_INIT;
695748
struct argv_array argv = ARGV_ARRAY_INIT;
696-
struct string_list_item *name_for_path;
749+
const char *name_for_path;
697750
const char *work_tree = get_git_work_tree();
698751
if (!work_tree)
699752
goto out;
@@ -724,18 +777,17 @@ int fetch_populated_submodules(const struct argv_array *options,
724777
continue;
725778

726779
name = ce->name;
727-
name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
780+
name_for_path = get_name_for_path(ce->name);
728781
if (name_for_path)
729-
name = name_for_path->util;
782+
name = name_for_path;
730783

731784
default_argv = "yes";
732785
if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
733-
struct string_list_item *fetch_recurse_submodules_option;
734-
fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
735-
if (fetch_recurse_submodules_option) {
736-
if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
786+
int fetch_recurse_option = get_fetch_recurse_for_name(name);
787+
if (fetch_recurse_option != RECURSE_SUBMODULES_NONE) {
788+
if (fetch_recurse_option == RECURSE_SUBMODULES_OFF)
737789
continue;
738-
if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
790+
if (fetch_recurse_option == RECURSE_SUBMODULES_ON_DEMAND) {
739791
if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
740792
continue;
741793
default_argv = "on-demand";

0 commit comments

Comments
 (0)