Skip to content

Commit 4d7f7a4

Browse files
jrngitster
authored andcommitted
diff: split off a function for --stat-* option parsing
As an optimization, the diff_opt_parse() switchboard has a single case for all the --stat-* options. Split it off into a separate function so we can enhance it without bringing code dangerously close to the right margin. Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dea007f commit 4d7f7a4

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

diff.c

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,6 +3029,38 @@ int parse_long_opt(const char *opt, const char **argv,
30293029
return 2;
30303030
}
30313031

3032+
static int stat_opt(struct diff_options *options, const char **av)
3033+
{
3034+
const char *arg = av[0];
3035+
char *end;
3036+
int width = options->stat_width;
3037+
int name_width = options->stat_name_width;
3038+
3039+
arg += strlen("--stat");
3040+
end = (char *)arg;
3041+
3042+
switch (*arg) {
3043+
case '-':
3044+
if (!prefixcmp(arg, "-width="))
3045+
width = strtoul(arg + 7, &end, 10);
3046+
else if (!prefixcmp(arg, "-name-width="))
3047+
name_width = strtoul(arg + 12, &end, 10);
3048+
break;
3049+
case '=':
3050+
width = strtoul(arg+1, &end, 10);
3051+
if (*end == ',')
3052+
name_width = strtoul(end+1, &end, 10);
3053+
}
3054+
3055+
/* Important! This checks all the error cases! */
3056+
if (*end)
3057+
return 0;
3058+
options->output_format |= DIFF_FORMAT_DIFFSTAT;
3059+
options->stat_name_width = name_width;
3060+
options->stat_width = width;
3061+
return 1;
3062+
}
3063+
30323064
int diff_opt_parse(struct diff_options *options, const char **av, int ac)
30333065
{
30343066
const char *arg = av[0];
@@ -3070,33 +3102,9 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
30703102
options->output_format |= DIFF_FORMAT_NAME_STATUS;
30713103
else if (!strcmp(arg, "-s"))
30723104
options->output_format |= DIFF_FORMAT_NO_OUTPUT;
3073-
else if (!prefixcmp(arg, "--stat")) {
3074-
char *end;
3075-
int width = options->stat_width;
3076-
int name_width = options->stat_name_width;
3077-
arg += 6;
3078-
end = (char *)arg;
3079-
3080-
switch (*arg) {
3081-
case '-':
3082-
if (!prefixcmp(arg, "-width="))
3083-
width = strtoul(arg + 7, &end, 10);
3084-
else if (!prefixcmp(arg, "-name-width="))
3085-
name_width = strtoul(arg + 12, &end, 10);
3086-
break;
3087-
case '=':
3088-
width = strtoul(arg+1, &end, 10);
3089-
if (*end == ',')
3090-
name_width = strtoul(end+1, &end, 10);
3091-
}
3092-
3093-
/* Important! This checks all the error cases! */
3094-
if (*end)
3095-
return 0;
3096-
options->output_format |= DIFF_FORMAT_DIFFSTAT;
3097-
options->stat_name_width = name_width;
3098-
options->stat_width = width;
3099-
}
3105+
else if (!prefixcmp(arg, "--stat"))
3106+
/* --stat, --stat-width, or --stat-name-width */
3107+
return stat_opt(options, av);
31003108

31013109
/* renames options */
31023110
else if (!prefixcmp(arg, "-B")) {

0 commit comments

Comments
 (0)