Skip to content

Commit b9632c9

Browse files
committed
Merge branch 'ao/config-from-gitmodules'
Tighten the API to make it harder to misuse in-tree .gitmodules file, even though it shares the same syntax with configuration files, to read random configuration items from it. * ao/config-from-gitmodules: submodule-config: reuse config_from_gitmodules in repo_read_gitmodules submodule-config: pass repository as argument to config_from_gitmodules submodule-config: make 'config_from_gitmodules' private submodule-config: add helper to get 'update-clone' config from .gitmodules submodule-config: add helper function to get 'fetch' config from .gitmodules config: move config_from_gitmodules to submodule-config.c
2 parents d18602f + db64d11 commit b9632c9

File tree

6 files changed

+80
-57
lines changed

6 files changed

+80
-57
lines changed

builtin/fetch.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,6 @@ static int git_fetch_config(const char *k, const char *v, void *cb)
9494
return git_default_config(k, v, cb);
9595
}
9696

97-
static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
98-
{
99-
if (!strcmp(var, "submodule.fetchjobs")) {
100-
max_children = parse_submodule_fetchjobs(var, value);
101-
return 0;
102-
} else if (!strcmp(var, "fetch.recursesubmodules")) {
103-
recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
104-
return 0;
105-
}
106-
107-
return 0;
108-
}
109-
11097
static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
11198
{
11299
/*
@@ -1434,7 +1421,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
14341421
for (i = 1; i < argc; i++)
14351422
strbuf_addf(&default_rla, " %s", argv[i]);
14361423

1437-
config_from_gitmodules(gitmodules_fetch_config, NULL);
1424+
fetch_config_from_gitmodules(&max_children, &recurse_submodules);
14381425
git_config(git_fetch_config, NULL);
14391426

14401427
argc = parse_options(argc, argv, prefix,

builtin/submodule--helper.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,8 +1708,8 @@ static int update_clone_task_finished(int result,
17081708
return 0;
17091709
}
17101710

1711-
static int gitmodules_update_clone_config(const char *var, const char *value,
1712-
void *cb)
1711+
static int git_update_clone_config(const char *var, const char *value,
1712+
void *cb)
17131713
{
17141714
int *max_jobs = cb;
17151715
if (!strcmp(var, "submodule.fetchjobs"))
@@ -1759,8 +1759,8 @@ static int update_clone(int argc, const char **argv, const char *prefix)
17591759
};
17601760
suc.prefix = prefix;
17611761

1762-
config_from_gitmodules(gitmodules_update_clone_config, &max_jobs);
1763-
git_config(gitmodules_update_clone_config, &max_jobs);
1762+
update_clone_config_from_gitmodules(&max_jobs);
1763+
git_config(git_update_clone_config, &max_jobs);
17641764

17651765
argc = parse_options(argc, argv, prefix, module_update_clone_options,
17661766
git_submodule_helper_usage, 0);

config.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,23 +2173,6 @@ int git_config_get_pathname(const char *key, const char **dest)
21732173
return repo_config_get_pathname(the_repository, key, dest);
21742174
}
21752175

2176-
/*
2177-
* Note: This function exists solely to maintain backward compatibility with
2178-
* 'fetch' and 'update_clone' storing configuration in '.gitmodules' and should
2179-
* NOT be used anywhere else.
2180-
*
2181-
* Runs the provided config function on the '.gitmodules' file found in the
2182-
* working directory.
2183-
*/
2184-
void config_from_gitmodules(config_fn_t fn, void *data)
2185-
{
2186-
if (the_repository->worktree) {
2187-
char *file = repo_worktree_path(the_repository, GITMODULES_FILE);
2188-
git_config_from_file(fn, file, data);
2189-
free(file);
2190-
}
2191-
}
2192-
21932176
int git_config_get_expiry(const char *key, const char **output)
21942177
{
21952178
int ret = git_config_get_string_const(key, output);

config.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,6 @@ extern int repo_config_get_maybe_bool(struct repository *repo,
215215
extern int repo_config_get_pathname(struct repository *repo,
216216
const char *key, const char **dest);
217217

218-
/*
219-
* Note: This function exists solely to maintain backward compatibility with
220-
* 'fetch' and 'update_clone' storing configuration in '.gitmodules' and should
221-
* NOT be used anywhere else.
222-
*
223-
* Runs the provided config function on the '.gitmodules' file found in the
224-
* working directory.
225-
*/
226-
extern void config_from_gitmodules(config_fn_t fn, void *data);
227-
228218
extern int git_config_get_value(const char *key, const char **value);
229219
extern const struct string_list *git_config_get_value_multi(const char *key);
230220
extern void git_config_clear(void);

submodule-config.c

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,23 @@ static void submodule_cache_check_init(struct repository *repo)
592592
submodule_cache_init(repo->submodule_cache);
593593
}
594594

595+
/*
596+
* Note: This function is private for a reason, the '.gitmodules' file should
597+
* not be used as as a mechanism to retrieve arbitrary configuration stored in
598+
* the repository.
599+
*
600+
* Runs the provided config function on the '.gitmodules' file found in the
601+
* working directory.
602+
*/
603+
static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
604+
{
605+
if (repo->worktree) {
606+
char *file = repo_worktree_path(repo, GITMODULES_FILE);
607+
git_config_from_file(fn, file, data);
608+
free(file);
609+
}
610+
}
611+
595612
static int gitmodules_cb(const char *var, const char *value, void *data)
596613
{
597614
struct repository *repo = data;
@@ -609,19 +626,11 @@ void repo_read_gitmodules(struct repository *repo)
609626
{
610627
submodule_cache_check_init(repo);
611628

612-
if (repo->worktree) {
613-
char *gitmodules;
614-
615-
if (repo_read_index(repo) < 0)
616-
return;
617-
618-
gitmodules = repo_worktree_path(repo, GITMODULES_FILE);
619-
620-
if (!is_gitmodules_unmerged(repo->index))
621-
git_config_from_file(gitmodules_cb, gitmodules, repo);
629+
if (repo_read_index(repo) < 0)
630+
return;
622631

623-
free(gitmodules);
624-
}
632+
if (!is_gitmodules_unmerged(repo->index))
633+
config_from_gitmodules(gitmodules_cb, repo, repo);
625634

626635
repo->submodule_cache->gitmodules_read = 1;
627636
}
@@ -672,3 +681,45 @@ void submodule_free(struct repository *r)
672681
if (r->submodule_cache)
673682
submodule_cache_clear(r->submodule_cache);
674683
}
684+
685+
struct fetch_config {
686+
int *max_children;
687+
int *recurse_submodules;
688+
};
689+
690+
static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
691+
{
692+
struct fetch_config *config = cb;
693+
if (!strcmp(var, "submodule.fetchjobs")) {
694+
*(config->max_children) = parse_submodule_fetchjobs(var, value);
695+
return 0;
696+
} else if (!strcmp(var, "fetch.recursesubmodules")) {
697+
*(config->recurse_submodules) = parse_fetch_recurse_submodules_arg(var, value);
698+
return 0;
699+
}
700+
701+
return 0;
702+
}
703+
704+
void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
705+
{
706+
struct fetch_config config = {
707+
.max_children = max_children,
708+
.recurse_submodules = recurse_submodules
709+
};
710+
config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
711+
}
712+
713+
static int gitmodules_update_clone_config(const char *var, const char *value,
714+
void *cb)
715+
{
716+
int *max_jobs = cb;
717+
if (!strcmp(var, "submodule.fetchjobs"))
718+
*max_jobs = parse_submodule_fetchjobs(var, value);
719+
return 0;
720+
}
721+
722+
void update_clone_config_from_gitmodules(int *max_jobs)
723+
{
724+
config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);
725+
}

submodule-config.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define SUBMODULE_CONFIG_CACHE_H
33

44
#include "cache.h"
5+
#include "config.h"
56
#include "hashmap.h"
67
#include "submodule.h"
78
#include "strbuf.h"
@@ -55,4 +56,15 @@ void submodule_free(struct repository *r);
5556
*/
5657
int check_submodule_name(const char *name);
5758

59+
/*
60+
* Note: these helper functions exist solely to maintain backward
61+
* compatibility with 'fetch' and 'update_clone' storing configuration in
62+
* '.gitmodules'.
63+
*
64+
* New helpers to retrieve arbitrary configuration from the '.gitmodules' file
65+
* should NOT be added.
66+
*/
67+
extern void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules);
68+
extern void update_clone_config_from_gitmodules(int *max_jobs);
69+
5870
#endif /* SUBMODULE_CONFIG_H */

0 commit comments

Comments
 (0)