builtin: move builtin retrieval to get_builtin()
There was a redundant code for a builtin command retrieval in 'handle_builtin()' and 'is_builtin()'. Introduce a new function 'get_builtin()' and using it from both of these places to reduce the redundancy. Signed-off-by: Slavomir Vlcek <svlc@inventati.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Slavomir Vlcek committed
Nov 12, 2014 at 14:10 UTC
c4f901d1593f3ef097c3e73daa2847ed9ad9efe0
1 file changed
+15
-12
git.c
+15
-12
@@ -486,15 +486,20 @@ static struct cmd_struct commands[] = {
486
{ "write-tree", cmd_write_tree, RUN_SETUP },
487
};
488
489
-int is_builtin(const char *s)
489
+static struct cmd_struct *get_builtin(const char *s)
490
{
491
int i;
492
for (i = 0; i < ARRAY_SIZE(commands); i++) {
493
- struct cmd_struct *p = commands+i;
493
+ struct cmd_struct *p = commands + i;
494
if (!strcmp(s, p->cmd))
495
- return 1;
495
+ return p;
496
}
497
- return 0;
497
+ return NULL;
498
+}
499
+
500
+int is_builtin(const char *s)
501
+{
502
+ return !!get_builtin(s);
503
}
504
505
static void handle_builtin(int argc, const char **argv)
@@ -502,6 +507,7 @@ static void handle_builtin(int argc, const char **argv)
507
const char *cmd = argv[0];
508
int i;
509
static const char ext[] = STRIP_EXTENSION;
510
+ struct cmd_struct *builtin;
511
512
if (sizeof(ext) > 1) {
513
i = strlen(argv[0]) - strlen(ext);
@@ -518,15 +524,12 @@ static void handle_builtin(int argc, const char **argv)
524
argv[0] = cmd = "help";
525
}
526
521
- for (i = 0; i < ARRAY_SIZE(commands); i++) {
522
- struct cmd_struct *p = commands+i;
523
- if (strcmp(p->cmd, cmd))
524
- continue;
525
- if (saved_environment && (p->option & NO_SETUP)) {
527
+ builtin = get_builtin(cmd);
528
+ if (builtin) {
529
+ if (saved_environment && (builtin->option & NO_SETUP))
530
restore_env();
527
- break;
528
- }
529
- exit(run_builtin(p, argc, argv));
531
+ else
532
+ exit(run_builtin(builtin, argc, argv));
533
}
534
}
535