submodule: factor out a config_set_in_gitmodules_file_gently function

Introduce a new config_set_in_gitmodules_file_gently() function to write config values to the .gitmodules file. This is in preparation for a future change which will use the function to write to the .gitmodules file in a more controlled way instead of using "git config -f .gitmodules". The purpose of the change is mainly to centralize the code that writes to the .gitmodules file to avoid some duplication. The naming follows git_config_set_in_file_gently() but the git_ prefix is removed to communicate that this is not a generic git-config API. Signed-off-by: Antonio Ospite <ao2@ao2.it> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Antonio Ospite committed Oct 5, 2018 at 15:05 UTC 45f5ef3d77ec3d2465641cd219b2f3874fa72083
3 files changed +16 -7
submodule-config.c
+12
@@ -707,6 +707,18 @@ int print_config_from_gitmodules(struct repository *repo, const char *key)
707 return 0;
708 }
709
710 +int config_set_in_gitmodules_file_gently(const char *key, const char *value)
711 +{
712 + int ret;
713 +
714 + ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
715 + if (ret < 0)
716 + /* Maybe the user already did that, don't error out here */
717 + warning(_("Could not update .gitmodules entry %s"), key);
718 +
719 + return ret;
720 +}
721 +
722 struct fetch_config {
723 int *max_children;
724 int *recurse_submodules;
submodule-config.h
+1
@@ -49,6 +49,7 @@ const struct submodule *submodule_from_path(struct repository *r,
49 const char *path);
50 void submodule_free(struct repository *r);
51 int print_config_from_gitmodules(struct repository *repo, const char *key);
52 +int config_set_in_gitmodules_file_gently(const char *key, const char *value);
53
54 /*
55 * Returns 0 if the name is syntactically acceptable as a submodule "name"
submodule.c
+3 -7
@@ -89,6 +89,7 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)
89 {
90 struct strbuf entry = STRBUF_INIT;
91 const struct submodule *submodule;
92 + int ret;
93
94 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
95 return -1;
@@ -104,14 +105,9 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)
105 strbuf_addstr(&entry, "submodule.");
106 strbuf_addstr(&entry, submodule->name);
107 strbuf_addstr(&entry, ".path");
107 - if (git_config_set_in_file_gently(GITMODULES_FILE, entry.buf, newpath) < 0) {
108 - /* Maybe the user already did that, don't error out here */
109 - warning(_("Could not update .gitmodules entry %s"), entry.buf);
110 - strbuf_release(&entry);
111 - return -1;
112 - }
108 + ret = config_set_in_gitmodules_file_gently(entry.buf, newpath);
109 strbuf_release(&entry);
114 - return 0;
110 + return ret;
111 }
112
113 /*