Create a helper function update_url_in_gitmodules() to update URL of an entry in .gitmodules. Later on, we want to use this function to aid in the conversion of 'set-url' from shell to C. Signed-off-by: Shourya Shukla <shouryashukla.oo@xxxxxxxxx> --- submodule.c | 33 +++++++++++++++++++++++++++++++++ submodule.h | 2 ++ 2 files changed, 35 insertions(+) diff --git a/submodule.c b/submodule.c index c3aadf3fff..0b599dc4e1 100644 --- a/submodule.c +++ b/submodule.c @@ -126,6 +126,39 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath) return ret; } +/* + * Try to update the "url" entry in the "submodule.<name>" section of the + * .gitmodules file. Return 0 only if a .gitmodules file was found, a section + * with the correct url=<oldurl> setting was found and we could update it. + */ +int update_url_in_gitmodules(const char *path, const char *newurl) +{ + struct strbuf entry = STRBUF_INIT; + const struct submodule *submodule; + int ret; + + /* Do nothing without .gitmodules */ + if (!file_exists(GITMODULES_FILE)) + return -1; + + if (is_gitmodules_unmerged(the_repository->index)) + die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); + + submodule = submodule_from_path(the_repository, &null_oid, path); + if (!submodule || !submodule->name) { + warning(_("Could not find section in .gitmodules where path=%s"), path); + return -1; + } + + strbuf_addstr(&entry, "submodule."); + strbuf_addstr(&entry, submodule->name); + strbuf_addstr(&entry, ".url"); + ret = config_set_in_gitmodules_file_gently(entry.buf, newurl); + strbuf_release(&entry); + + return ret; +} + /* * Try to remove the "submodule.<name>" section from .gitmodules where the given * path is configured. Return 0 only if a .gitmodules file was found, a section diff --git a/submodule.h b/submodule.h index 4dad649f94..ea09480433 100644 --- a/submodule.h +++ b/submodule.h @@ -49,6 +49,8 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *, const char *path); int git_default_submodule_config(const char *var, const char *value, void *cb); +int update_url_in_gitmodules(const char* path, const char *newurl); + struct option; int option_parse_recurse_submodules_worktree_updater(const struct option *opt, const char *arg, int unset); -- 2.20.1