git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH/GSoC_v3] branch.c: turn nested if-else logic to table-driven

From
YZYao Zhao <zhaox383@umn.edu>
Date
Mar 16, 2014, 06:08 UTC
Message-ID
<1394950103-2264-1-git-send-email-zhaox383@umn.edu>
In-Reply-To
<CAPig+cRF_eQiGugR8TSks5ki375y-5wiQ7HWKyKRudJ5apd4cg@mail.gmail.com>
Signed-off-by: Yao Zhao <zhaox383@umn.edu>
---
 branch.c | 53 +++++++++++++++++++++++++++++------------------------
 1 file changed, 29 insertions(+), 24 deletions(-)
Hello Eric,
Thank you and Junio for reviewing my code. It is really helpful to improve my code quality.
This is version 3 of patch. Previous address : http://thread.gmane.org/gmane.comp.version-control.git/243919. I do not use positional initializer because it is not allowed to use variable in it. I don't know if it's ok to use this redundant way to initialize "list".
I cannot find -v flag in documentation you indicated in last email so I use set-prefix to add it into prefix.
	
Now I am working on writing proposal for git project. I am really interested in last one, about improve git_config. I know it's important to get known about git_config first and have read documentation about it. But I am really confused about how to understand code of git_config. When user type in git config in terminal, what is the execute order of functions? How git config influence other git command? Does program read config file every time when they execuate config-related command?
Thank you,
Yao
diff --git a/branch.c b/branch.c
index 723a36b..1df30c7 100644
--- a/branch.c
+++ b/branch.c
@@ -53,7 +53,33 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
 	int remote_is_branch = starts_with(remote, "refs/heads/");
 	struct strbuf key = STRBUF_INIT;
 	int rebasing = should_setup_rebase(origin);
-
+	struct print_list {
+		const char *print_str;
+		const char *arg2; 
+		const char *arg3;
+	} ; 
+	struct print_list target;
+
+	struct print_list list[2][2][2];
+	list[0][0][0].print_str = N_("Branch %s set up to track local ref %s.");
+	list[0][0][0].arg2 = remote;
+	list[0][0][1].print_str = N_("Branch %s set up to track local ref %s by rebasing.");
+	list[0][0][1].arg2 = remote;
+	list[0][1][0].print_str = N_("Branch %s set up to track remote ref %s.");
+	list[0][1][0].arg2 = remote;
+	list[0][1][1].print_str = N_("Branch %s set up to track remote ref %s by rebasing.");
+	list[0][1][1].arg2 = remote;
+	list[1][0][0].print_str = N_("Branch %s set up to track local branch %s.");
+	list[1][0][0].arg2 =shortname;
+	list[1][0][1].print_str = N_("Branch %s set up to track local branch %s by rebasing.");
+	list[1][0][1].arg2 = shortname;
+	list[1][1][0].print_str = N_("Branch %s set up to track remote branch %s from %s.");
+	list[1][1][0].arg2 = shortname;
+	list[1][1][0].arg3 = origin;
+	list[1][1][1].print_str = N_("Branch %s set up to track remote branch %s from %s by rebasing.");
+	list[1][1][1].arg2 = shortname;
+	list[1][1][1].arg3 = origin;
+ 
 	if (remote_is_branch
 	    && !strcmp(local, shortname)
 	    && !origin) {
@@ -77,29 +103,8 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
 	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
-			die("BUG: impossible combination of %d and %p",
-			    remote_is_branch, origin);
+			target = list[!!remote_is_branch][!!origin][!!rebasing];
+			printf_ln (_(target.print_str), local, target.arg2, target.arg3);
 	}
 }
 
-- 
1.8.3.2
Previous: Quint GuvernatorNext: Eric Sunshine
Message 12 of 13 in “SoC 2014 MicroProject No.8:change multiple if-else statement to table-driven approach”
  1. SoC 2014 MicroProject No.8:change multiple if-else statement to table-driven approachYao Zhao, Mar 12, 2014
  2. Eric SunshineMar 12, 2014
  3. GSoC Change multiple if-else statements to be table-drivenYao Zhao, Mar 13, 2014
  4. Eric SunshineMar 14, 2014
  5. Junio C HamanoMar 14, 2014
  6. Eric SunshineMar 17, 2014
  7. Junio C HamanoMar 17, 2014
  8. Michael HaggertyMar 17, 2014
  9. Junio C HamanoMar 17, 2014
  10. Eric SunshineMar 17, 2014
  11. Quint GuvernatorMar 17, 2014
  12. branch.c: turn nested if-else logic to table-drivenYao Zhao, Mar 16, 2014
  13. Eric SunshineMar 17, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.