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

[PATCH v4 4/7] remote rename/remove: handle branch.<name>.pushRemote config values

From
Bert Wesarg <bert.wesarg@googlemail.com>
Date
Jan 24, 2020, 09:25 UTC
Message-ID
<9c2110eda64ab7bf0cb780dafe1f13b28fee5ca0.1579857394.git.bert.wesarg@googlemail.com>
In-Reply-To
<cover.1579857394.git.bert.wesarg@googlemail.com>
When renaming or removing a remote with
    git remote rename X Y
    git remote remove X
Git already renames/removes any config values from
    branch.<name>.remote = X
to
    branch.<name>.remote = Y

As branch.<name>.pushRemote also names a remote, it now also renames or removes these config values from

    branch.<name>.pushRemote = X
to
    branch.<name>.pushRemote = Y
Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>
---
Changes since v3:
 * handle also 'git remote remove'
Cc: Junio C Hamano <gitster@pobox.com>
Cc: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin/remote.c  | 22 +++++++++++++++++++++-
 t/t5505-remote.sh | 16 +++++++++++++++-
 2 files changed, 36 insertions(+), 2 deletions(-)
diff --git a/builtin/remote.c b/builtin/remote.c
index 9ee44c9f6c..a2379a14bf 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -250,6 +250,7 @@ struct branch_info {
 	char *remote_name;
 	struct string_list merge;
 	enum rebase_type rebase;
+	char *push_remote_name;
 };
 
 static struct string_list branch_list = STRING_LIST_INIT_NODUP;
@@ -267,7 +268,7 @@ static int config_read_branches(const char *key, const char *value, void *cb)
 	char *name;
 	struct string_list_item *item;
 	struct branch_info *info;
-	enum { REMOTE, MERGE, REBASE } type;
+	enum { REMOTE, MERGE, REBASE, PUSH_REMOTE } type;
 	size_t key_len;
 
 	if (!starts_with(key, "branch."))
@@ -280,6 +281,8 @@ static int config_read_branches(const char *key, const char *value, void *cb)
 		type = MERGE;
 	else if (strip_suffix(key, ".rebase", &key_len))
 		type = REBASE;
+	else if (strip_suffix(key, ".pushremote", &key_len))
+		type = PUSH_REMOTE;
 	else
 		return 0;
 	name = xmemdupz(key, key_len);
@@ -315,6 +318,11 @@ static int config_read_branches(const char *key, const char *value, void *cb)
 		 */
 		info->rebase = rebase_parse_value(value);
 		break;
+	case PUSH_REMOTE:
+		if (info->push_remote_name)
+			warning(_("more than one %s"), orig_key);
+		info->push_remote_name = xstrdup(value);
+		break;
 	default:
 		BUG("unexpected type=%d", type);
 	}
@@ -682,6 +690,11 @@ static int mv(int argc, const char **argv)
 			strbuf_addf(&buf, "branch.%s.remote", item->string);
 			git_config_set(buf.buf, rename.new_name);
 		}
+		if (info->push_remote_name && !strcmp(info->push_remote_name, rename.old_name)) {
+			strbuf_reset(&buf);
+			strbuf_addf(&buf, "branch.%s.pushremote", item->string);
+			git_config_set(buf.buf, rename.new_name);
+		}
 	}
 
 	if (!refspec_updated)
@@ -783,6 +796,13 @@ static int rm(int argc, const char **argv)
 					die(_("could not unset '%s'"), buf.buf);
 			}
 		}
+		if (info->push_remote_name && !strcmp(info->push_remote_name, remote->name)) {
+			strbuf_reset(&buf);
+			strbuf_addf(&buf, "branch.%s.pushremote", item->string);
+			result = git_config_set_gently(buf.buf, NULL);
+			if (result && result != CONFIG_NOTHING_SET)
+				die(_("could not unset '%s'"), buf.buf);
+		}
 	}
 
 	/*
diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh
index 883b32efa0..082042b05a 100755
--- a/t/t5505-remote.sh
+++ b/t/t5505-remote.sh
@@ -737,12 +737,14 @@ test_expect_success 'rename a remote' '
 	git clone one four &&
 	(
 		cd four &&
+		git config branch.master.pushRemote origin &&
 		git remote rename origin upstream &&
 		test -z "$(git for-each-ref refs/remotes/origin)" &&
 		test "$(git symbolic-ref refs/remotes/upstream/HEAD)" = "refs/remotes/upstream/master" &&
 		test "$(git rev-parse upstream/master)" = "$(git rev-parse master)" &&
 		test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/remotes/upstream/*" &&
-		test "$(git config branch.master.remote)" = "upstream"
+		test "$(git config branch.master.remote)" = "upstream" &&
+		test "$(git config branch.master.pushRemote)" = "upstream"
 	)
 '
 
@@ -784,6 +786,18 @@ test_expect_success 'rename succeeds with existing remote.<target>.prune' '
 	git -C four.four remote rename origin upstream
 '
 
+test_expect_success 'remove a remote' '
+	git clone one four.five &&
+	(
+		cd four.five &&
+		git config branch.master.pushRemote origin &&
+		git remote remove origin &&
+		test -z "$(git for-each-ref refs/remotes/origin)" &&
+		test_must_fail git config branch.master.remote &&
+		test_must_fail git config branch.master.pushRemote
+	)
+'
+
 cat >remotes_origin <<EOF
 URL: $(pwd)/one
 Push: refs/heads/master:refs/heads/upstream
-- 
2.24.1.497.g9abd7b20b4.dirty
Previous: Bert WesargNext: Johannes Schindelin
Message 6 of 14 in “remote rename/remove: improve handling of configuration values”
  1. 0/7 remote rename/remove: improve handling of configuration valuesBert Wesarg, Jan 24, 2020
  2. 2/7 remote: clean-up by returning early to avoid one indentationBert Wesarg, Jan 24, 2020
  3. 1/7 pull --rebase/remote rename: document and honor single-letter abbreviations rebase typesBert Wesarg, Jan 24, 2020
  4. 3/7 remote: clean-up config callbackBert Wesarg, Jan 24, 2020
  5. 5/7 config: make `scope_name` global as `config_scope_name`Bert Wesarg, Jan 24, 2020
  6. 4/7 remote rename/remove: handle branch.<name>.pushRemote config valuesBert Wesarg, Jan 24, 2020
  7. Johannes SchindelinJan 25, 2020
  8. Bert WesargJan 25, 2020
  9. Johannes SchindelinJan 26, 2020
  10. 7/7 remote rename/remove: gently handle remote.pushDefault configBert Wesarg, Jan 24, 2020
  11. 6/7 config: provide access to the current line numberBert Wesarg, Jan 24, 2020
  12. Junio C HamanoJan 24, 2020
  13. Matt RogersJan 25, 2020
  14. Bert WesargJan 27, 2020

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.