git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dragos Foianu <dragos.foianu@gmail.com>
To: git@vger.kernel.org
Cc: Dragos Foianu <dragos.foianu@gmail.com>
Subject: [PATCHv2] branch.c: simplify chain of if statements
Date: Mon, 17 Mar 2014 17:51:33 +0200	[thread overview]
Message-ID: <1395071493-31435-1-git-send-email-dragos.foianu@gmail.com> (raw)

This patch uses a table to store the different messages that can
be emitted by the verbose install_branch_config function. It
computes an index based on the three flags and prints the message
located at the specific index in the table of messages. If the
index somehow is not within the table, we have a bug.

Signed-off-by: Dragos Foianu <dragos.foianu@gmail.com>
---
 branch.c | 44 +++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/branch.c b/branch.c
index 723a36b..95645d5 100644
--- a/branch.c
+++ b/branch.c
@@ -54,6 +54,18 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
 	struct strbuf key = STRBUF_INIT;
 	int rebasing = should_setup_rebase(origin);
 
+	const char *messages[] = {
+		N_("Branch %s set up to track local ref %s."),
+		N_("Branch %s set up to track remote ref %s."),
+		N_("Branch %s set up to track local branch %s."),
+		N_("Branch %s set up to track remote branch %s from %s."),
+		N_("Branch %s set up to track local ref %s by rebasing."),
+		N_("Branch %s set up to track remote ref %s by rebasing."),
+		N_("Branch %s set up to track local branch %s by rebasing."),
+		N_("Branch %s set up to track remote branch %s from %s by rebasing.")
+	};
+	int index = 0;
+
 	if (remote_is_branch
 	    && !strcmp(local, shortname)
 	    && !origin) {
@@ -76,28 +88,22 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
 	}
 	strbuf_release(&key);
 
+	if (origin)
+		index += 1;
+	if (remote_is_branch)
+		index += 2;
+	if (rebasing)
+		index += 4;
+
 	if (flag & BRANCH_CONFIG_VERBOSE) {
 		if (remote_is_branch && 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 if (remote_is_branch && !origin)
-			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);
-		else if (!remote_is_branch && 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 if (!remote_is_branch && !origin)
-			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);
+			printf_ln(_(messages[index]),
+				local, shortname, origin);
 		else
+			printf_ln(_(messages[index]),
+				local, (!remote_is_branch) ? remote : shortname);
+
+		if (index < 0 || index > sizeof(messages) / sizeof(*messages))
 			die("BUG: impossible combination of %d and %p",
 			    remote_is_branch, origin);
 	}
-- 
1.8.3.2

             reply	other threads:[~2014-03-17 15:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-17 15:51 Dragos Foianu [this message]
2014-03-18 22:31 ` [PATCHv2] branch.c: simplify chain of if statements Eric Sunshine
2014-03-18 23:50   ` Eric Sunshine
2014-03-19 23:12     ` Dragos Foianu
2014-03-21  0:40       ` Eric Sunshine
2014-03-21  0:44         ` Eric Sunshine
2014-03-21 16:50       ` Junio C Hamano

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=1395071493-31435-1-git-send-email-dragos.foianu@gmail.com \
    --to=dragos.foianu@gmail.com \
    --cc=git@vger.kernel.org \
    /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 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 NNTP newsgroup(s).