@@ -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
@@ -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);
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@gmail.com> --- submodule.c | 33 +++++++++++++++++++++++++++++++++ submodule.h | 2 ++ 2 files changed, 35 insertions(+)