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

[PATCH v6 09/15] remote: die on config error when manipulating remotes

From
Patrick Steinhardt <ps@pks.im>
Date
Feb 22, 2016, 11:23 UTC
Message-ID
<1456140216-24169-10-git-send-email-ps@pks.im>
In-Reply-To
<1456140216-24169-1-git-send-email-ps@pks.im>

When manipulating remotes we try to set various configuration values without checking if the values were persisted correctly, possibly leaving the remote in an inconsistent state.

Fix this issue by dying early and notifying the user about the error.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/remote.c | 39 ++++++++++++---------------------------
 1 file changed, 12 insertions(+), 27 deletions(-)
diff --git a/builtin/remote.c b/builtin/remote.c
index eeb6d2e..fe57f77 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -197,8 +197,7 @@ static int add(int argc, const char **argv)
 		die(_("'%s' is not a valid remote name"), name);
 
 	strbuf_addf(&buf, "remote.%s.url", name);
-	if (git_config_set(buf.buf, url))
-		return 1;
+	git_config_set_or_die(buf.buf, url);
 
 	if (!mirror || mirror & MIRROR_FETCH) {
 		strbuf_reset(&buf);
@@ -214,16 +213,14 @@ static int add(int argc, const char **argv)
 	if (mirror & MIRROR_PUSH) {
 		strbuf_reset(&buf);
 		strbuf_addf(&buf, "remote.%s.mirror", name);
-		if (git_config_set(buf.buf, "true"))
-			return 1;
+		git_config_set_or_die(buf.buf, "true");
 	}
 
 	if (fetch_tags != TAGS_DEFAULT) {
 		strbuf_reset(&buf);
 		strbuf_addf(&buf, "remote.%s.tagopt", name);
-		if (git_config_set(buf.buf,
-			fetch_tags == TAGS_SET ? "--tags" : "--no-tags"))
-			return 1;
+		git_config_set_or_die(buf.buf,
+				      fetch_tags == TAGS_SET ? "--tags" : "--no-tags");
 	}
 
 	if (fetch && fetch_remote(name))
@@ -591,25 +588,20 @@ static int migrate_file(struct remote *remote)
 
 	strbuf_addf(&buf, "remote.%s.url", remote->name);
 	for (i = 0; i < remote->url_nr; i++)
-		if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0))
-			return error(_("Could not append '%s' to '%s'"),
-					remote->url[i], buf.buf);
+		git_config_set_multivar_or_die(buf.buf, remote->url[i], "^$", 0);
 	strbuf_reset(&buf);
 	strbuf_addf(&buf, "remote.%s.push", remote->name);
 	for (i = 0; i < remote->push_refspec_nr; i++)
-		if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0))
-			return error(_("Could not append '%s' to '%s'"),
-					remote->push_refspec[i], buf.buf);
+		git_config_set_multivar_or_die(buf.buf, remote->push_refspec[i], "^$", 0);
 	strbuf_reset(&buf);
 	strbuf_addf(&buf, "remote.%s.fetch", remote->name);
 	for (i = 0; i < remote->fetch_refspec_nr; i++)
-		if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0))
-			return error(_("Could not append '%s' to '%s'"),
-					remote->fetch_refspec[i], buf.buf);
+		git_config_set_multivar_or_die(buf.buf, remote->fetch_refspec[i], "^$", 0);
 	if (remote->origin == REMOTE_REMOTES)
 		unlink_or_warn(git_path("remotes/%s", remote->name));
 	else if (remote->origin == REMOTE_BRANCHES)
 		unlink_or_warn(git_path("branches/%s", remote->name));
+
 	return 0;
 }
 
@@ -656,8 +648,7 @@ static int mv(int argc, const char **argv)
 
 	strbuf_reset(&buf);
 	strbuf_addf(&buf, "remote.%s.fetch", rename.new);
-	if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
-		return error(_("Could not remove config section '%s'"), buf.buf);
+	git_config_set_multivar_or_die(buf.buf, NULL, NULL, 1);
 	strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old);
 	for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
 		char *ptr;
@@ -677,8 +668,7 @@ static int mv(int argc, const char **argv)
 				  "\tPlease update the configuration manually if necessary."),
 				buf2.buf);
 
-		if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
-			return error(_("Could not append '%s'"), buf.buf);
+		git_config_set_multivar_or_die(buf.buf, buf2.buf, "^$", 0);
 	}
 
 	read_branches();
@@ -688,9 +678,7 @@ static int mv(int argc, const char **argv)
 		if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
 			strbuf_reset(&buf);
 			strbuf_addf(&buf, "branch.%s.remote", item->string);
-			if (git_config_set(buf.buf, rename.new)) {
-				return error(_("Could not set '%s'"), buf.buf);
-			}
+			git_config_set_or_die(buf.buf, rename.new);
 		}
 	}
 
@@ -788,10 +776,7 @@ static int rm(int argc, const char **argv)
 				strbuf_reset(&buf);
 				strbuf_addf(&buf, "branch.%s.%s",
 						item->string, *k);
-				if (git_config_set(buf.buf, NULL)) {
-					strbuf_release(&buf);
-					return -1;
-				}
+				git_config_set_or_die(buf.buf, NULL);
 			}
 		}
 	}
-- 
2.7.1
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 10 of 16 in “config: make git_config_set die on failure”
  1. 00/15 config: make git_config_set die on failurePatrick Steinhardt, Feb 22, 2016
  2. 01/15 config: introduce set_or_die wrappersPatrick Steinhardt, Feb 22, 2016
  3. 02/15 branch: report errors in tracking branch setupPatrick Steinhardt, Feb 22, 2016
  4. 03/15 branch: die on config error when unsetting upstreamPatrick Steinhardt, Feb 22, 2016
  5. 04/15 branch: die on config error when editing branch descriptionPatrick Steinhardt, Feb 22, 2016
  6. 05/15 submodule: die on config error when linking modulesPatrick Steinhardt, Feb 22, 2016
  7. 06/15 submodule--helper: die on config error when cloning modulePatrick Steinhardt, Feb 22, 2016
  8. 07/15 remote: die on config error when setting URLPatrick Steinhardt, Feb 22, 2016
  9. 08/15 remote: die on config error when setting/adding branchesPatrick Steinhardt, Feb 22, 2016
  10. 09/15 remote: die on config error when manipulating remotesPatrick Steinhardt, Feb 22, 2016
  11. 10/15 clone: die on config error in cmd_clonePatrick Steinhardt, Feb 22, 2016
  12. 11/15 init-db: die on config errors when initializing empty repoPatrick Steinhardt, Feb 22, 2016
  13. 12/15 sequencer: die on config error when saving replay optsPatrick Steinhardt, Feb 22, 2016
  14. 13/15 compat: die when unable to set core.precomposeunicodePatrick Steinhardt, Feb 22, 2016
  15. 14/15 config: rename git_config_set to git_config_set_gentlyPatrick Steinhardt, Feb 22, 2016
  16. 15/15 config: rename git_config_set_or_die to git_config_setPatrick Steinhardt, Feb 22, 2016

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.