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 <jrnieder@gmail.com> Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Aug 5, 2010 at 10:22 UTC
4d7f7a4ae7cf6caeab6aedc798b6528a4fb88829
1 file changed
+35
-27
diff.c
+35
-27
@@ -3029,6 +3029,38 @@ int parse_long_opt(const char *opt, const char **argv,
3029
return 2;
3030
}
3031
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
+
3064
int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3065
{
3066
const char *arg = av[0];
@@ -3070,33 +3102,9 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3102
options->output_format |= DIFF_FORMAT_NAME_STATUS;
3103
else if (!strcmp(arg, "-s"))
3104
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);
3108
3109
/* renames options */
3110
else if (!prefixcmp(arg, "-B")) {