Skip to content

Commit c4f901d

Browse files
svlcgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7fa1365 commit c4f901d

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

git.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -486,22 +486,28 @@ static struct cmd_struct commands[] = {
486486
{ "write-tree", cmd_write_tree, RUN_SETUP },
487487
};
488488

489-
int is_builtin(const char *s)
489+
static struct cmd_struct *get_builtin(const char *s)
490490
{
491491
int i;
492492
for (i = 0; i < ARRAY_SIZE(commands); i++) {
493-
struct cmd_struct *p = commands+i;
493+
struct cmd_struct *p = commands + i;
494494
if (!strcmp(s, p->cmd))
495-
return 1;
495+
return p;
496496
}
497-
return 0;
497+
return NULL;
498+
}
499+
500+
int is_builtin(const char *s)
501+
{
502+
return !!get_builtin(s);
498503
}
499504

500505
static void handle_builtin(int argc, const char **argv)
501506
{
502507
const char *cmd = argv[0];
503508
int i;
504509
static const char ext[] = STRIP_EXTENSION;
510+
struct cmd_struct *builtin;
505511

506512
if (sizeof(ext) > 1) {
507513
i = strlen(argv[0]) - strlen(ext);
@@ -518,15 +524,12 @@ static void handle_builtin(int argc, const char **argv)
518524
argv[0] = cmd = "help";
519525
}
520526

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))
526530
restore_env();
527-
break;
528-
}
529-
exit(run_builtin(p, argc, argv));
531+
else
532+
exit(run_builtin(builtin, argc, argv));
530533
}
531534
}
532535

0 commit comments

Comments
 (0)