git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH/RFC] builtin: move builtin retrieval to get_builtin()
@ 2014-11-12 22:07 slavomir vlcek
  2014-11-13  0:29 ` [PATCH] SubmittingPatches: fix an inconsistency slavomir vlcek
  2014-11-13 18:19 ` [PATCH/RFC] builtin: move builtin retrieval to get_builtin() Junio C Hamano
  0 siblings, 2 replies; 9+ messages in thread
From: slavomir vlcek @ 2014-11-12 22:07 UTC (permalink / raw)
  To: git, sschuberth

Hi,
found a small code redundancy in a builtin command retrieval ('git.c').

For the "master" branch.

Thanks in advance for any suggestions.

Signed-off-by: slavomir vlcek <svlc@inventati.org>
---

>From 78228e3f7c3029d07827f973fa7992777d6e0cb9 Mon Sep 17 00:00:00 2001
From: slavomir vlcek <svlc@inventati.org>
Date: Wed, 12 Nov 2014 14:10:22 +0100
Subject: [PATCH] builtin: move builtin retrieval to get_builtin()

There was a redundant code for a builtin command retrieval
in 'handle_builtin()' and 'is_builtin()'.

This was solved by adding a new function 'get_builtin()'
and by making a boolean wrapper out of the 'is_builtin()'.
---


 git.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/git.c b/git.c
index 18fbf79..e32c5b8 100644
--- a/git.c
+++ b/git.c
@@ -487,15 +487,20 @@ static struct cmd_struct commands[] = {
 	{ "write-tree", cmd_write_tree, RUN_SETUP },
 };
 
-int is_builtin(const char *s)
+struct cmd_struct *get_builtin(const char *s)
 {
 	int i;
 	for (i = 0; i < ARRAY_SIZE(commands); i++) {
-		struct cmd_struct *p = commands+i;
+		struct cmd_struct *p = commands + i;
 		if (!strcmp(s, p->cmd))
-			return 1;
+			return p;
 	}
-	return 0;
+	return NULL;
+}
+
+int is_builtin(const char *s)
+{
+	return !!get_builtin(s);
 }
 
 static void handle_builtin(int argc, const char **argv)
@@ -503,6 +508,7 @@ static void handle_builtin(int argc, const char **argv)
 	const char *cmd = argv[0];
 	int i;
 	static const char ext[] = STRIP_EXTENSION;
+	struct cmd_struct *builtin;
 
 	if (sizeof(ext) > 1) {
 		i = strlen(argv[0]) - strlen(ext);
@@ -519,15 +525,12 @@ static void handle_builtin(int argc, const char **argv)
 		argv[0] = cmd = "help";
 	}
 
-	for (i = 0; i < ARRAY_SIZE(commands); i++) {
-		struct cmd_struct *p = commands+i;
-		if (strcmp(p->cmd, cmd))
-			continue;
-		if (saved_environment && (p->option & NO_SETUP)) {
+	builtin = get_builtin(cmd);
+	if (builtin) {
+		if (saved_environment && (builtin->option & NO_SETUP))
 			restore_env();
-			break;
-		}
-		exit(run_builtin(p, argc, argv));
+		else
+			exit(run_builtin(builtin, argc, argv));
 	}
 }
 
-- 
2.0.1

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2014-11-17 16:42 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-12 22:07 [PATCH/RFC] builtin: move builtin retrieval to get_builtin() slavomir vlcek
2014-11-13  0:29 ` [PATCH] SubmittingPatches: fix an inconsistency slavomir vlcek
2014-11-13 18:20   ` Junio C Hamano
2014-11-13 18:28     ` Junio C Hamano
2014-11-13 18:30       ` Junio C Hamano
2014-11-13 21:41         ` slavomir vlcek
2014-11-13 18:19 ` [PATCH/RFC] builtin: move builtin retrieval to get_builtin() Junio C Hamano
2014-11-16 23:33   ` Slavomir Vlcek
2014-11-17 16:42     ` Junio C Hamano

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).