All of lore.kernel.org
 help / color / mirror / Atom feed
From: Denton Liu <liu.denton@gmail.com>
To: git@vger.kernel.org
Cc: liu.denton@gmail.com, anmolmago@gmail.com, briankyho@gmail.com,
	david.lu97@outlook.com, shirui.wang@hotmail.com,
	f.francet@hotmail.com
Subject: [RFC PATCH] remote: add --fetch option to git remote set-url
Date: Sat, 27 Oct 2018 04:09:07 -0400	[thread overview]
Message-ID: <1d1b0fe85ddd89cf8172e730e8886d5b4a9ea7eb.1540627720.git.liu.denton@gmail.com> (raw)

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


             reply	other threads:[~2018-10-27  8:09 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-27  8:09 Denton Liu [this message]
2018-10-29  5:57 ` [RFC PATCH] remote: add --fetch option to git remote set-url 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1d1b0fe85ddd89cf8172e730e8886d5b4a9ea7eb.1540627720.git.liu.denton@gmail.com \
    --to=liu.denton@gmail.com \
    --cc=anmolmago@gmail.com \
    --cc=briankyho@gmail.com \
    --cc=david.lu97@outlook.com \
    --cc=f.francet@hotmail.com \
    --cc=git@vger.kernel.org \
    --cc=shirui.wang@hotmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.