git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Yao Zhao <zhaox383@umn.edu>
To: sunshine@sunshineco.com
Cc: git@vger.kernel.org, Yao Zhao <zhaox383@umn.edu>
Subject: [PATCH] GSoC Change multiple if-else statements to be table-driven
Date: Thu, 13 Mar 2014 15:20:59 -0500	[thread overview]
Message-ID: <1394742059-7300-1-git-send-email-zhaox383@umn.edu> (raw)
In-Reply-To: <CAPig+cQu7D3AUghOSUOZBwf5+iHCPkxPbY1WuQmPJk1muCk7tQ@mail.gmail.com>

Signed-off-by: Yao Zhao <zhaox383@umn.edu>
---
GSoC_MicroProject_#8

Hello Eric,

Thanks for reviewing my code. I implemented table-driven method this time and correct the style
problems indicated in review.

Thank you,

Yao

 branch.c | 72 ++++++++++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 50 insertions(+), 22 deletions(-)

diff --git a/branch.c b/branch.c
index 723a36b..6451c99 100644
--- a/branch.c
+++ b/branch.c
@@ -49,10 +49,43 @@ static int should_setup_rebase(const char *origin)
 
 void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
 {
+
 	const char *shortname = remote + 11;
 	int remote_is_branch = starts_with(remote, "refs/heads/");
 	struct strbuf key = STRBUF_INIT;
 	int rebasing = should_setup_rebase(origin);
+	int size=8, i;
+
+	typedef struct PRINT_LIST {
+		const char *print_str;
+		const char *arg2; 
+		//arg1 is always local, so I only add arg2 and arg3 in struct
+		const char *arg3;
+		int b_rebasing;
+	  int b_remote_is_branch;
+		int b_origin;
+	} PRINT_LIST;
+
+	PRINT_LIST print_list[] = {
+		{.print_str = _("Branch %s set up to track remote branch %s from %s by rebasing."), 
+				.arg2 = shortname, .arg3 = origin,
+					 .b_rebasing = 1, .b_remote_is_branch = 1, .b_origin = 1},
+	  {.print_str = _("Branch %s set up to track remote branch %s from %s."), 
+				.arg2 = shortname, .arg3 = origin,
+					 .b_rebasing = 0, .b_remote_is_branch = 1, .b_origin = 1},
+    {.print_str = _("Branch %s set up to track local branch %s by rebasing."), 
+				.arg2 = shortname, .b_rebasing = 1, .b_remote_is_branch = 1, .b_origin = 0},
+		{.print_str = _("Branch %s set up to track local branch %s."), 
+				.arg2 = shortname, .b_rebasing = 0, .b_remote_is_branch = 1, .b_origin = 0},
+		{.print_str = _("Branch %s set up to track remote ref %s by rebasing."), 
+				.arg2 = remote, .b_rebasing = 1, .b_remote_is_branch = 0, .b_origin = 1},
+		{.print_str = _("Branch %s set up to track remote ref %s."), 
+				.arg2 = remote, .b_rebasing = 0, .b_remote_is_branch = 0, .b_origin = 1},
+		{.print_str = _("Branch %s set up to track local ref %s by rebasing."), 
+				.arg2 = remote, .b_rebasing = 1, .b_remote_is_branch = 0, .b_origin = 0},
+		{.print_str = _("Branch %s set up to track local ref %s."), 
+				.arg2 = remote, .b_rebasing = 0, .b_remote_is_branch = 0, .b_origin = 0},
+};
I am confused here: I use struct initializer and I am not sure if it's ok
because it is only supported by ANSI 

 	if (remote_is_branch
 	    && !strcmp(local, shortname)
@@ -75,31 +108,26 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
 		git_config_set(key.buf, "true");
 	}
 	strbuf_release(&key);
-
 	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);
-		else
+		for (i=0;i<size;i++)
+		{
+			if (print_list[i].b_rebasing == (rebasing? 1 : 0) && 
+								print_list[i].b_remote_is_branch == (remote_is_branch? 1 : 0) &&
+								print_list[i].b_origin == (origin? 1 : 0))
+			{
+				if (print_list[i].arg3 == NULL)
+					printf_ln (print_list[i].print_str, local, print_list[i].arg2);
+				else
+					printf_ln (print_list[i].print_str, local, 
+							print_list[i].arg2, print_list[i].arg3);
+				
+				break;
+			}
+		}
+		if (i == size)
 			die("BUG: impossible combination of %d and %p",
 			    remote_is_branch, origin);
+
 	}
 }
 
-- 
1.8.3.2

  reply	other threads:[~2014-03-13 20:21 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-12  3:47 [PATCH] SoC 2014 MicroProject No.8:change multiple if-else statement to table-driven approach Yao Zhao
2014-03-12  9:21 ` Eric Sunshine
2014-03-13 20:20   ` Yao Zhao [this message]
2014-03-14  2:16     ` [PATCH] GSoC Change multiple if-else statements to be table-driven Eric Sunshine
2014-03-14 16:54       ` Junio C Hamano
2014-03-17  6:29         ` Eric Sunshine
2014-03-17  7:31           ` Junio C Hamano
2014-03-17 14:11             ` Michael Haggerty
2014-03-17 19:12               ` Junio C Hamano
2014-03-17 19:27               ` Eric Sunshine
2014-03-17 20:39                 ` Quint Guvernator
2014-03-16  6:08       ` [PATCH/GSoC_v3] branch.c: turn nested if-else logic to table-driven Yao Zhao
2014-03-17  7:54         ` Eric Sunshine

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=1394742059-7300-1-git-send-email-zhaox383@umn.edu \
    --to=zhaox383@umn.edu \
    --cc=git@vger.kernel.org \
    --cc=sunshine@sunshineco.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.
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).