git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [RFC PATCH] remote: add --fetch option to git remote set-url
@ 2018-10-27  8:09 Denton Liu
  2018-10-29  5:57 ` Junio C Hamano
  0 siblings, 1 reply; 9+ messages in thread
From: Denton Liu @ 2018-10-27  8:09 UTC (permalink / raw)
  To: git; +Cc: liu.denton, anmolmago, briankyho, david.lu97, shirui.wang,
	f.francet

This adds the --fetch option to `git remote set-url` such that when
executed we move the remote.*.url to remote.*.pushurl and set
remote.*.url to the given url argument.

For example, if we have the following config:

	[remote "origin"]
		url = git@github.com:git/git.git

`git remote set-url --fetch origin https://github.com/git/git.git`
would change the config to the following:

	[remote "origin"]
		url = https://github.com/git/git.git
		pushurl = git@github.com:git/git.git

Signed-off-by: Denton Liu <liu.denton@gmail.com>
Signed-off-by: Filip Francetic <f.francet@hotmail.com>
---
 builtin/remote.c | 42 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 34 insertions(+), 8 deletions(-)

diff --git a/builtin/remote.c b/builtin/remote.c
index f7edf7f2c..fcf1220c6 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -23,9 +23,9 @@ static const char * const builtin_remote_usage[] = {
 	N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
 	N_("git remote set-branches [--add] <name> <branch>..."),
 	N_("git remote get-url [--push] [--all] <name>"),
-	N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
-	N_("git remote set-url --add <name> <newurl>"),
-	N_("git remote set-url --delete <name> <url>"),
+	N_("git remote set-url [--push|--fetch] <name> <newurl> [<oldurl>]"),
+	N_("git remote set-url --add [--push|--fetch] <name> <newurl>"),
+	N_("git remote set-url --delete [--push|--fetch] <name> <url>"),
 	NULL
 };
 
@@ -76,9 +76,9 @@ static const char * const builtin_remote_geturl_usage[] = {
 };
 
 static const char * const builtin_remote_seturl_usage[] = {
-	N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
-	N_("git remote set-url --add <name> <newurl>"),
-	N_("git remote set-url --delete <name> <url>"),
+	N_("git remote set-url [--push|--fetch] <name> <newurl> [<oldurl>]"),
+	N_("git remote set-url --add [--push|--fetch] <name> <newurl>"),
+	N_("git remote set-url --delete [--push|--fetch] <name> <url>"),
 	NULL
 };
 
@@ -1519,7 +1519,7 @@ static int get_url(int argc, const char **argv)
 
 static int set_url(int argc, const char **argv)
 {
-	int i, push_mode = 0, add_mode = 0, delete_mode = 0;
+	int i, push_mode = 0, fetch_mode = 0, add_mode = 0, delete_mode = 0, move_fetch_to_push = 0;
 	int matches = 0, negative_matches = 0;
 	const char *remotename = NULL;
 	const char *newurl = NULL;
@@ -1532,6 +1532,8 @@ static int set_url(int argc, const char **argv)
 	struct option options[] = {
 		OPT_BOOL('\0', "push", &push_mode,
 			 N_("manipulate push URLs")),
+		OPT_BOOL('\0', "fetch", &fetch_mode,
+			 N_("manipulate fetch URLs")),
 		OPT_BOOL('\0', "add", &add_mode,
 			 N_("add URL")),
 		OPT_BOOL('\0', "delete", &delete_mode,
@@ -1543,6 +1545,8 @@ static int set_url(int argc, const char **argv)
 
 	if (add_mode && delete_mode)
 		die(_("--add --delete doesn't make sense"));
+	if (push_mode && fetch_mode)
+		die(_("--push --fetch doesn't make sense"));
 
 	if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
 		usage_with_options(builtin_remote_seturl_usage, options);
@@ -1559,18 +1563,40 @@ static int set_url(int argc, const char **argv)
 	if (!remote_is_configured(remote, 1))
 		die(_("No such remote '%s'"), remotename);
 
+	/*
+	 * If add_mode, we will be appending to remote.*.url so we shouldn't move the urls over.
+	 * If pushurls exist, we don't need to move the urls over to pushurl.
+	 */
+	move_fetch_to_push = fetch_mode && !add_mode && !remote->pushurl_nr;
+
 	if (push_mode) {
 		strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
 		urlset = remote->pushurl;
 		urlset_nr = remote->pushurl_nr;
 	} else {
+		if (move_fetch_to_push) {
+			strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
+			for (i = 0; i < remote->url_nr; i++) {
+				git_config_set_multivar(name_buf.buf, remote->url[i],
+						"^$", 0);
+			}
+			strbuf_reset(&name_buf);
+		}
+
 		strbuf_addf(&name_buf, "remote.%s.url", remotename);
 		urlset = remote->url;
 		urlset_nr = remote->url_nr;
 	}
 
+	/* Empty fetch URLs if they are being replaced */
+	if (move_fetch_to_push) {
+		for (i = 0; i < remote->url_nr; i++) {
+			git_config_set_multivar(name_buf.buf, NULL, remote->url[i], 1);
+		}
+	}
+
 	/* Special cases that add new entry. */
-	if ((!oldurl && !delete_mode) || add_mode) {
+	if ((!oldurl && !delete_mode) || move_fetch_to_push || add_mode) {
 		if (add_mode)
 			git_config_set_multivar(name_buf.buf, newurl,
 						       "^$", 0);
-- 
2.19.1.542.gc4df23f79.dirty


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2018-12-10 14:15 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-27  8:09 [RFC PATCH] remote: add --fetch option to git remote set-url Denton Liu
2018-10-29  5:57 ` Junio C Hamano
2018-10-30  7:56   ` Denton Liu
2018-10-30 10:11     ` Junio C Hamano
2018-11-09  2:37       ` [RFC PATCH v2] remote: add --save-push " Denton Liu
2018-11-09  3:15         ` Junio C Hamano
2018-11-09  5:20           ` [PATCH v3] remote: add --save-to-push " Denton Liu
2018-11-13  9:46             ` Junio C Hamano
2018-12-10 14:15             ` [PATCH v4] " Denton Liu

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).