git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v5 2/9] submodule: factor out a config_set_in_gitmodules_file_gently function

From
Antonio Ospite <ao2@ao2.it>
Date
Sep 17, 2018, 14:09 UTC
Message-ID
<20180917140940.3839-3-ao2@ao2.it>
In-Reply-To
<20180917140940.3839-1-ao2@ao2.it>

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>
---
 submodule-config.c | 12 ++++++++++++
 submodule-config.h |  1 +
 submodule.c        | 10 +++-------
 3 files changed, 16 insertions(+), 7 deletions(-)
diff --git a/submodule-config.c b/submodule-config.c
index f70b7f1baf..61a555e920 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -707,6 +707,18 @@ int print_config_from_gitmodules(const char *key)
 	return 0;
 }
 
+int config_set_in_gitmodules_file_gently(const char *key, const char *value)
+{
+	int ret;
+
+	ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
+	if (ret < 0)
+		/* Maybe the user already did that, don't error out here */
+		warning(_("Could not update .gitmodules entry %s"), key);
+
+	return ret;
+}
+
 struct fetch_config {
 	int *max_children;
 	int *recurse_submodules;
diff --git a/submodule-config.h b/submodule-config.h
index dd7f1b9a46..3921927aa1 100644
--- a/submodule-config.h
+++ b/submodule-config.h
@@ -49,6 +49,7 @@ const struct submodule *submodule_from_path(struct repository *r,
 					    const char *path);
 void submodule_free(struct repository *r);
 int print_config_from_gitmodules(const char *key);
+int config_set_in_gitmodules_file_gently(const char *key, const char *value);
 
 /*
  * Returns 0 if the name is syntactically acceptable as a submodule "name"
diff --git a/submodule.c b/submodule.c
index a2b266fbfa..2e97032f86 100644
--- a/submodule.c
+++ b/submodule.c
@@ -89,6 +89,7 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)
 {
 	struct strbuf entry = STRBUF_INIT;
 	const struct submodule *submodule;
+	int ret;
 
 	if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
 		return -1;
@@ -104,14 +105,9 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)
 	strbuf_addstr(&entry, "submodule.");
 	strbuf_addstr(&entry, submodule->name);
 	strbuf_addstr(&entry, ".path");
-	if (git_config_set_in_file_gently(GITMODULES_FILE, entry.buf, newpath) < 0) {
-		/* Maybe the user already did that, don't error out here */
-		warning(_("Could not update .gitmodules entry %s"), entry.buf);
-		strbuf_release(&entry);
-		return -1;
-	}
+	ret = config_set_in_gitmodules_file_gently(entry.buf, newpath);
 	strbuf_release(&entry);
-	return 0;
+	return ret;
 }
 
 /*
-- 
2.19.0
Previous: Antonio OspiteNext: Antonio Ospite
Message 18 of 23 in “Make submodules work if .gitmodules is not checked out”
  1. 0/9 Make submodules work if .gitmodules is not checked outAntonio Ospite, Sep 17, 2018
  2. 9/9 submodule: support reading .gitmodules when it's not in the working treeAntonio Ospite, Sep 17, 2018
  3. SZEDER GáborSep 18, 2018
  4. Junio C HamanoSep 19, 2018
  5. Antonio OspiteSep 20, 2018
  6. Junio C HamanoSep 21, 2018
  7. Antonio OspiteSep 27, 2018
  8. Antonio OspiteSep 24, 2018
  9. Stefan BellerSep 24, 2018
  10. Antonio OspiteSep 27, 2018
  11. Stefan BellerSep 27, 2018
  12. Antonio OspiteOct 1, 2018
  13. Stefan BellerOct 1, 2018
  14. 6/9 submodule: use the 'submodule--helper config' commandAntonio Ospite, Sep 17, 2018
  15. 3/9 t7411: merge tests 5 and 6Antonio Ospite, Sep 17, 2018
  16. 5/9 submodule--helper: add a new 'config' subcommandAntonio Ospite, Sep 17, 2018
  17. 7/9 t7506: clean up .gitmodules properly before setting up new scenarioAntonio Ospite, Sep 17, 2018
  18. 2/9 submodule: factor out a config_set_in_gitmodules_file_gently functionAntonio Ospite, Sep 17, 2018
  19. 4/9 t7411: be nicer to future tests and really clean things upAntonio Ospite, Sep 17, 2018
  20. 8/9 submodule: add a helper to check if it is safe to write to .gitmodulesAntonio Ospite, Sep 17, 2018
  21. 1/9 submodule: add a print_config_from_gitmodules() helperAntonio Ospite, Sep 17, 2018
  22. Antonio OspiteSep 24, 2018
  23. Stefan BellerSep 24, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.