summaryrefslogtreecommitdiff
path: root/builtin/submodule--helper.c
diff options
context:
space:
mode:
Diffstat (limited to 'builtin/submodule--helper.c')
-rw-r--r--builtin/submodule--helper.c102
1 files changed, 70 insertions, 32 deletions
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index d8a6fa47e5..07a1935cbe 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -32,6 +32,8 @@
#include "advice.h"
#include "branch.h"
#include "list-objects-filter-options.h"
+#include "wildmatch.h"
+#include "strbuf.h"
#define OPT_QUIET (1 << 0)
#define OPT_CACHED (1 << 1)
@@ -53,7 +55,7 @@ static char *resolve_relative_url(const char *rel_url, const char *up_path, int
struct strbuf remotesb = STRBUF_INIT;
strbuf_addf(&remotesb, "remote.%s.url", remote);
- if (git_config_get_string(remotesb.buf, &remoteurl)) {
+ if (repo_config_get_string(the_repository, remotesb.buf, &remoteurl)) {
if (!quiet)
warning(_("could not look up configuration '%s'. "
"Assuming this repository is its own "
@@ -458,7 +460,7 @@ static void init_submodule(const char *path, const char *prefix,
*/
if (!is_submodule_active(the_repository, path)) {
strbuf_addf(&sb, "submodule.%s.active", sub->name);
- git_config_set_gently(sb.buf, "true");
+ repo_config_set_gently(the_repository, sb.buf, "true");
strbuf_reset(&sb);
}
@@ -468,7 +470,7 @@ static void init_submodule(const char *path, const char *prefix,
* .gitmodules, so look it up directly.
*/
strbuf_addf(&sb, "submodule.%s.url", sub->name);
- if (git_config_get_string(sb.buf, &url)) {
+ if (repo_config_get_string(the_repository, sb.buf, &url)) {
if (!sub->url)
die(_("No url found for submodule path '%s' in .gitmodules"),
displaypath);
@@ -484,7 +486,7 @@ static void init_submodule(const char *path, const char *prefix,
free(oldurl);
}
- if (git_config_set_gently(sb.buf, url))
+ if (repo_config_set_gently(the_repository, sb.buf, url))
die(_("Failed to register url for submodule path '%s'"),
displaypath);
if (!(flags & OPT_QUIET))
@@ -496,7 +498,7 @@ static void init_submodule(const char *path, const char *prefix,
/* Copy "update" setting when it is not set yet */
strbuf_addf(&sb, "submodule.%s.update", sub->name);
- if (git_config_get_string_tmp(sb.buf, &upd) &&
+ if (repo_config_get_string_tmp(the_repository, sb.buf, &upd) &&
sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
@@ -506,7 +508,7 @@ static void init_submodule(const char *path, const char *prefix,
upd = submodule_update_type_to_string(sub->update_strategy.type);
}
- if (git_config_set_gently(sb.buf, upd))
+ if (repo_config_set_gently(the_repository, sb.buf, upd))
die(_("Failed to register update mode for submodule path '%s'"), displaypath);
}
strbuf_release(&sb);
@@ -549,7 +551,7 @@ static int module_init(int argc, const char **argv, const char *prefix,
* If there are no path args and submodule.active is set then,
* by default, only initialize 'active' modules.
*/
- if (!argc && !git_config_get("submodule.active"))
+ if (!argc && !repo_config_get(the_repository, "submodule.active"))
module_list_active(&list);
info.prefix = prefix;
@@ -649,7 +651,7 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
"--ignore-submodules=dirty", "--quiet", "--",
path, NULL);
- git_config(git_diff_basic_config, NULL);
+ repo_config(the_repository, git_diff_basic_config, NULL);
repo_init_revisions(the_repository, &rev, NULL);
rev.abbrev = 0;
@@ -1034,7 +1036,7 @@ static void prepare_submodule_summary(struct summary_cb *info,
config_key = xstrfmt("submodule.%s.ignore",
sub->name);
- if (!git_config_get_string_tmp(config_key, &value))
+ if (!repo_config_get_string_tmp(the_repository, config_key, &value))
ignore_all = !strcmp(value, "all");
else if (sub->ignore)
ignore_all = !strcmp(sub->ignore, "all");
@@ -1108,7 +1110,7 @@ static int compute_summary_module_list(struct object_id *head_oid,
if (info->argc)
strvec_pushv(&diff_args, info->argv);
- git_config(git_diff_basic_config, NULL);
+ repo_config(the_repository, git_diff_basic_config, NULL);
repo_init_revisions(the_repository, &rev, info->prefix);
rev.abbrev = 0;
precompose_argv_prefix(diff_args.nr, diff_args.v, NULL);
@@ -1262,7 +1264,7 @@ static void sync_submodule(const char *path, const char *prefix,
strbuf_reset(&sb);
strbuf_addf(&sb, "submodule.%s.url", sub->name);
- if (git_config_set_gently(sb.buf, super_config_url))
+ if (repo_config_set_gently(the_repository, sb.buf, super_config_url))
die(_("failed to register url for submodule path '%s'"),
displaypath);
@@ -1280,7 +1282,7 @@ static void sync_submodule(const char *path, const char *prefix,
submodule_to_gitdir(the_repository, &sb, path);
strbuf_addstr(&sb, "/config");
- if (git_config_set_in_file_gently(sb.buf, remote_key, NULL, sub_origin_url))
+ if (repo_config_set_in_file_gently(the_repository, sb.buf, remote_key, NULL, sub_origin_url))
die(_("failed to update remote for submodule '%s'"),
path);
@@ -1623,11 +1625,11 @@ static void prepare_possible_alternates(const char *sm_name,
char *sm_alternate = NULL, *error_strategy = NULL;
struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
- git_config_get_string("submodule.alternateLocation", &sm_alternate);
+ repo_config_get_string(the_repository, "submodule.alternateLocation", &sm_alternate);
if (!sm_alternate)
return;
- git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
+ repo_config_get_string(the_repository, "submodule.alternateErrorStrategy", &error_strategy);
if (!error_strategy)
error_strategy = xstrdup("die");
@@ -1808,14 +1810,14 @@ static int clone_submodule(const struct module_clone_data *clone_data,
die(_("could not get submodule directory for '%s'"), clone_data_path);
/* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
- git_config_get_string("submodule.alternateLocation", &sm_alternate);
+ repo_config_get_string(the_repository, "submodule.alternateLocation", &sm_alternate);
if (sm_alternate)
- git_config_set_in_file(p, "submodule.alternateLocation",
- sm_alternate);
- git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
+ repo_config_set_in_file(the_repository, p, "submodule.alternateLocation",
+ sm_alternate);
+ repo_config_get_string(the_repository, "submodule.alternateErrorStrategy", &error_strategy);
if (error_strategy)
- git_config_set_in_file(p, "submodule.alternateErrorStrategy",
- error_strategy);
+ repo_config_set_in_file(the_repository, p, "submodule.alternateErrorStrategy",
+ error_strategy);
free(sm_alternate);
free(error_strategy);
@@ -2522,7 +2524,7 @@ static int ensure_core_worktree(const char *path)
abs_path = absolute_pathdup(path);
rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
- git_config_set_in_file(cfg_file, "core.worktree", rel_path);
+ repo_config_set_in_file(the_repository, cfg_file, "core.worktree", rel_path);
free(cfg_file);
free(abs_path);
@@ -2830,7 +2832,7 @@ static int module_update(int argc, const char **argv, const char *prefix,
};
update_clone_config_from_gitmodules(&opt.max_jobs);
- git_config(git_update_clone_config, &opt.max_jobs);
+ repo_config(the_repository, git_update_clone_config, &opt.max_jobs);
argc = parse_options(argc, argv, prefix, module_update_options,
git_submodule_helper_usage, 0);
@@ -2878,7 +2880,7 @@ static int module_update(int argc, const char **argv, const char *prefix,
* If there are no path args and submodule.active is set then,
* by default, only initialize 'active' modules.
*/
- if (!argc && !git_config_get("submodule.active"))
+ if (!argc && !repo_config_get(the_repository, "submodule.active"))
module_list_active(&list);
info.prefix = opt.prefix;
@@ -3128,7 +3130,7 @@ static int module_create_branch(int argc, const char **argv, const char *prefix,
NULL
};
- git_config(git_default_config, NULL);
+ repo_config(the_repository, git_default_config, NULL);
track = git_branch_track;
argc = parse_options(argc, argv, prefix, options, usage, 0);
@@ -3307,9 +3309,11 @@ static void configure_added_submodule(struct add_data *add_data)
char *key;
struct child_process add_submod = CHILD_PROCESS_INIT;
struct child_process add_gitmodules = CHILD_PROCESS_INIT;
+ const struct string_list *values;
+ int matched = 0;
key = xstrfmt("submodule.%s.url", add_data->sm_name);
- git_config_set_gently(key, add_data->realrepo);
+ repo_config_set_gently(the_repository, key, add_data->realrepo);
free(key);
add_submod.git_cmd = 1;
@@ -3349,20 +3353,28 @@ static void configure_added_submodule(struct add_data *add_data)
* is_submodule_active(), since that function needs to find
* out the value of "submodule.active" again anyway.
*/
- if (!git_config_get("submodule.active")) {
+ if (repo_config_get(the_repository, "submodule.active") || /* key absent */
+ repo_config_get_string_multi(the_repository, "submodule.active", &values)) {
/*
* If the submodule being added isn't already covered by the
* current configured pathspec, set the submodule's active flag
*/
- if (!is_submodule_active(the_repository, add_data->sm_path)) {
+ key = xstrfmt("submodule.%s.active", add_data->sm_name);
+ repo_config_set_gently(the_repository, key, "true");
+ free(key);
+ } else {
+ for (size_t i = 0; i < values->nr; i++) {
+ const char *pat = values->items[i].string;
+ if (!wildmatch(pat, add_data->sm_path, 0)) { /* match found */
+ matched = 1;
+ break;
+ }
+ }
+ if (!matched) { /* no pattern matched -> force-enable */
key = xstrfmt("submodule.%s.active", add_data->sm_name);
- git_config_set_gently(key, "true");
+ repo_config_set_gently(the_repository, key, "true");
free(key);
}
- } else {
- key = xstrfmt("submodule.%s.active", add_data->sm_name);
- git_config_set_gently(key, "true");
- free(key);
}
}
@@ -3423,6 +3435,9 @@ static int module_add(int argc, const char **argv, const char *prefix,
struct add_data add_data = ADD_DATA_INIT;
const char *ref_storage_format = NULL;
char *to_free = NULL;
+ const struct submodule *existing;
+ struct strbuf buf = STRBUF_INIT;
+ char *sm_name_to_free = NULL;
struct option options[] = {
OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
N_("branch of repository to add as submodule")),
@@ -3525,6 +3540,28 @@ static int module_add(int argc, const char **argv, const char *prefix,
if(!add_data.sm_name)
add_data.sm_name = add_data.sm_path;
+ existing = submodule_from_name(the_repository,
+ null_oid(the_hash_algo),
+ add_data.sm_name);
+
+ if (existing && strcmp(existing->path, add_data.sm_path)) {
+ if (!force) {
+ die(_("submodule name '%s' already used for path '%s'"),
+ add_data.sm_name, existing->path);
+ }
+ /* --force: build <name><n> until unique */
+ for (int i = 1; ; i++) {
+ strbuf_reset(&buf);
+ strbuf_addf(&buf, "%s%d", add_data.sm_name, i);
+ if (!submodule_from_name(the_repository,
+ null_oid(the_hash_algo),
+ buf.buf)) {
+ break;
+ }
+ }
+ add_data.sm_name = sm_name_to_free = strbuf_detach(&buf, NULL);
+ }
+
if (check_submodule_name(add_data.sm_name))
die(_("'%s' is not a valid submodule name"), add_data.sm_name);
@@ -3540,6 +3577,7 @@ static int module_add(int argc, const char **argv, const char *prefix,
ret = 0;
cleanup:
+ free(sm_name_to_free);
free(add_data.sm_path);
free(to_free);
strbuf_release(&sb);