[PATCH v5 1/2] branch: accept multiple upstream branches for tracking

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add a new static variant of install_branch_config() that accepts
multiple remote branch names for tracking. This will be used in an
upcoming commit that enables inheriting the tracking configuration from
a parent branch.

Currently, all callers of install_branch_config() pass only a single
remote. Make install_branch_config() a small wrapper around
install_branch_config_multiple_remotes() so that existing callers do not
need to be changed.

Signed-off-by: Josh Steadmon <steadmon@xxxxxxxxxx>
---
 branch.c | 120 ++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 87 insertions(+), 33 deletions(-)

diff --git a/branch.c b/branch.c
index 7a88a4861e..1aabef4de0 100644
--- a/branch.c
+++ b/branch.c
@@ -55,19 +55,24 @@ N_("\n"
 "the remote tracking information by invoking\n"
 "\"git branch --set-upstream-to=%s%s%s\".");
 
-int install_branch_config(int flag, const char *local, const char *origin, const char *remote)
+static int install_branch_config_multiple_remotes(int flag, const char *local, const char *origin,
+		struct string_list *remotes)
 {
 	const char *shortname = NULL;
 	struct strbuf key = STRBUF_INIT;
-	int rebasing = should_setup_rebase(origin);
-
-	if (skip_prefix(remote, "refs/heads/", &shortname)
-	    && !strcmp(local, shortname)
-	    && !origin) {
-		warning(_("Not setting branch %s as its own upstream."),
-			local);
-		return 0;
-	}
+	int i, rebasing = should_setup_rebase(origin);
+
+	if (remotes->nr < 1)
+		BUG("must provide at least one remote for branch config");
+
+	if (!origin)
+		for (i = 0; i < remotes->nr; i++)
+			if (skip_prefix(remotes->items[i].string, "refs/heads/", &shortname)
+			    && !strcmp(local, shortname)) {
+				warning(_("Not setting branch %s as its own upstream."),
+					local);
+				return 0;
+			}
 
 	strbuf_addf(&key, "branch.%s.remote", local);
 	if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
@@ -75,8 +80,17 @@ int install_branch_config(int flag, const char *local, const char *origin, const
 
 	strbuf_reset(&key);
 	strbuf_addf(&key, "branch.%s.merge", local);
-	if (git_config_set_gently(key.buf, remote) < 0)
+	/*
+	 * We want to overwrite any existing config with all the branches in
+	 * "remotes". Override any existing config with the first branch, but if
+	 * more than one is provided, use CONFIG_REGEX_NONE to preserve what
+	 * we've written so far.
+	 */
+	if (git_config_set_gently(key.buf, remotes->items[0].string) < 0)
 		goto out_err;
+	for (i = 1; i < remotes->nr; i++)
+		if (git_config_set_multivar_gently(key.buf, remotes->items[i].string, CONFIG_REGEX_NONE, 0) < 0)
+			goto out_err;
 
 	if (rebasing) {
 		strbuf_reset(&key);
@@ -87,29 +101,62 @@ int install_branch_config(int flag, const char *local, const char *origin, const
 	strbuf_release(&key);
 
 	if (flag & BRANCH_CONFIG_VERBOSE) {
-		if (shortname) {
-			if (origin)
-				printf_ln(rebasing ?
-					  _("Branch '%s' set up to track remote branch '%s' from '%s' by rebasing.") :
-					  _("Branch '%s' set up to track remote branch '%s' from '%s'."),
-					  local, shortname, origin);
-			else
-				printf_ln(rebasing ?
-					  _("Branch '%s' set up to track local branch '%s' by rebasing.") :
-					  _("Branch '%s' set up to track local branch '%s'."),
-					  local, shortname);
+		int plural = remotes->nr > 1;
+		int all_shortnames = 1;
+		const char *msg_fmt;
+		struct strbuf ref_string = STRBUF_INIT;
+
+		for (i = 0; i < remotes->nr; i++)
+			if (skip_prefix(remotes->items[i].string, "refs/heads/", &shortname)) {
+				strbuf_addf(&ref_string, "'%s', ", shortname);
+			} else {
+				all_shortnames = 0;
+				strbuf_addf(&ref_string, "'%s', ", remotes->items[i].string);
+			}
+		/* The last two characters are an extraneous ", ", so trim those. */
+		strbuf_setlen(&ref_string, ref_string.len - 2);
+
+		if (all_shortnames && origin) {
+			if (rebasing && plural)
+				msg_fmt = "Branch '%s' set up to track remote branches %s from '%s' by rebasing.";
+			else if (rebasing && !plural)
+				msg_fmt = "Branch '%s' set up to track remote branch %s from '%s' by rebasing.";
+			else if (!rebasing && plural)
+				msg_fmt = "Branch '%s' set up to track remote branches %s from '%s'.";
+			else if (!rebasing && !plural)
+				msg_fmt = "Branch '%s' set up to track remote branch %s from '%s'.";
+
+			printf_ln(_(msg_fmt), local, ref_string, origin);
 		} else {
-			if (origin)
-				printf_ln(rebasing ?
-					  _("Branch '%s' set up to track remote ref '%s' by rebasing.") :
-					  _("Branch '%s' set up to track remote ref '%s'."),
-					  local, remote);
-			else
-				printf_ln(rebasing ?
-					  _("Branch '%s' set up to track local ref '%s' by rebasing.") :
-					  _("Branch '%s' set up to track local ref '%s'."),
-					  local, remote);
+			if (all_shortnames && !origin && rebasing && plural)
+				msg_fmt = "Branch '%s' set up to track local branches %s by rebasing.";
+			if (all_shortnames && !origin && rebasing && !plural)
+				msg_fmt = "Branch '%s' set up to track local branch %s by rebasing.";
+			if (all_shortnames && !origin && !rebasing && plural)
+				msg_fmt = "Branch '%s' set up to track local branches %s.";
+			if (all_shortnames && !origin && !rebasing && !plural)
+				msg_fmt = "Branch '%s' set up to track local branch %s.";
+			if (!all_shortnames && origin && rebasing && plural)
+				msg_fmt = "Branch '%s' set up to track remote refs %s by rebasing.";
+			if (!all_shortnames && origin && rebasing && !plural)
+				msg_fmt = "Branch '%s' set up to track remote ref %s by rebasing.";
+			if (!all_shortnames && origin && !rebasing && plural)
+				msg_fmt = "Branch '%s' set up to track remote refs %s.";
+			if (!all_shortnames && origin && !rebasing && !plural)
+				msg_fmt = "Branch '%s' set up to track remote ref %s.";
+			if (!all_shortnames && !origin && rebasing && plural)
+				msg_fmt = "Branch '%s' set up to track local refs %s by rebasing.";
+			if (!all_shortnames && !origin && rebasing && !plural)
+				msg_fmt = "Branch '%s' set up to track local ref %s by rebasing.";
+			if (!all_shortnames && !origin && !rebasing && plural)
+				msg_fmt = "Branch '%s' set up to track local refs %s.";
+			if (!all_shortnames && !origin && !rebasing && !plural)
+				msg_fmt = "Branch '%s' set up to track local ref %s.";
+
+			printf_ln(_(msg_fmt), local, ref_string);
 		}
+
+		strbuf_release(&ref_string);
 	}
 
 	return 0;
@@ -121,11 +168,18 @@ int install_branch_config(int flag, const char *local, const char *origin, const
 	advise(_(tracking_advice),
 	       origin ? origin : "",
 	       origin ? "/" : "",
-	       shortname ? shortname : remote);
+	       remotes->items[0].string);
 
 	return -1;
 }
 
+int install_branch_config(int flag, const char *local, const char *origin, const char *remote) {
+	struct string_list remotes = STRING_LIST_INIT_DUP;
+	string_list_append(&remotes, remote);
+	return install_branch_config_multiple_remotes(flag, local, origin, &remotes);
+	string_list_clear(&remotes, 0);
+}
+
 /*
  * This is called when new_ref is branched off of orig_ref, and tries
  * to infer the settings for branch.<new_ref>.{remote,merge} from the
-- 
2.34.1.400.ga245620fadb-goog




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux