Skip to content

Commit dea007f

Browse files
moygitster
authored andcommitted
diff: parse separate options like -S foo
Change the option parsing logic in revision.c to accept separate forms like `-S foo' in addition to `-Sfoo'. The rest of git already accepted this form, but revision.c still used its own option parsing. Short options affected are -S<string>, -l<num> and -O<orderfile>, for which an empty string wouldn't make sense, hence -<option> <arg> isn't ambiguous. This patch does not handle --stat-name-width and --stat-width, which are special-cases where diff_long_opt do not apply. They are handled in a separate patch to ease review. Original patch by Matthieu Moy, plus refactoring by Jonathan Nieder. Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 64fdc08 commit dea007f

File tree

5 files changed

+95
-23
lines changed

5 files changed

+95
-23
lines changed

diff.c

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2990,9 +2990,50 @@ static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *va
29902990

29912991
static int diff_scoreopt_parse(const char *opt);
29922992

2993+
static inline int short_opt(char opt, const char **argv,
2994+
const char **optarg)
2995+
{
2996+
const char *arg = argv[0];
2997+
if (arg[0] != '-' || arg[1] != opt)
2998+
return 0;
2999+
if (arg[2] != '\0') {
3000+
*optarg = arg + 2;
3001+
return 1;
3002+
}
3003+
if (!argv[1])
3004+
die("Option '%c' requires a value", opt);
3005+
*optarg = argv[1];
3006+
return 2;
3007+
}
3008+
3009+
int parse_long_opt(const char *opt, const char **argv,
3010+
const char **optarg)
3011+
{
3012+
const char *arg = argv[0];
3013+
if (arg[0] != '-' || arg[1] != '-')
3014+
return 0;
3015+
arg += strlen("--");
3016+
if (prefixcmp(arg, opt))
3017+
return 0;
3018+
arg += strlen(opt);
3019+
if (*arg == '=') { /* sticked form: --option=value */
3020+
*optarg = arg + 1;
3021+
return 1;
3022+
}
3023+
if (*arg != '\0')
3024+
return 0;
3025+
/* separate form: --option value */
3026+
if (!argv[1])
3027+
die("Option '--%s' requires a value", opt);
3028+
*optarg = argv[1];
3029+
return 2;
3030+
}
3031+
29933032
int diff_opt_parse(struct diff_options *options, const char **av, int ac)
29943033
{
29953034
const char *arg = av[0];
3035+
const char *optarg;
3036+
int argcount;
29963037

29973038
/* Output format options */
29983039
if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch"))
@@ -3149,10 +3190,11 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
31493190
else
31503191
die("bad --word-diff argument: %s", type);
31513192
}
3152-
else if (!prefixcmp(arg, "--word-diff-regex=")) {
3193+
else if ((argcount = parse_long_opt("word-diff-regex", av, &optarg))) {
31533194
if (options->word_diff == DIFF_WORDS_NONE)
31543195
options->word_diff = DIFF_WORDS_PLAIN;
3155-
options->word_regex = arg + 18;
3196+
options->word_regex = optarg;
3197+
return argcount;
31563198
}
31573199
else if (!strcmp(arg, "--exit-code"))
31583200
DIFF_OPT_SET(options, EXIT_WITH_STATUS);
@@ -3180,18 +3222,26 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
31803222
/* misc options */
31813223
else if (!strcmp(arg, "-z"))
31823224
options->line_termination = 0;
3183-
else if (!prefixcmp(arg, "-l"))
3184-
options->rename_limit = strtoul(arg+2, NULL, 10);
3185-
else if (!prefixcmp(arg, "-S"))
3186-
options->pickaxe = arg + 2;
3225+
else if ((argcount = short_opt('l', av, &optarg))) {
3226+
options->rename_limit = strtoul(optarg, NULL, 10);
3227+
return argcount;
3228+
}
3229+
else if ((argcount = short_opt('S', av, &optarg))) {
3230+
options->pickaxe = optarg;
3231+
return argcount;
3232+
}
31873233
else if (!strcmp(arg, "--pickaxe-all"))
31883234
options->pickaxe_opts = DIFF_PICKAXE_ALL;
31893235
else if (!strcmp(arg, "--pickaxe-regex"))
31903236
options->pickaxe_opts = DIFF_PICKAXE_REGEX;
3191-
else if (!prefixcmp(arg, "-O"))
3192-
options->orderfile = arg + 2;
3193-
else if (!prefixcmp(arg, "--diff-filter="))
3194-
options->filter = arg + 14;
3237+
else if ((argcount = short_opt('O', av, &optarg))) {
3238+
options->orderfile = optarg;
3239+
return argcount;
3240+
}
3241+
else if ((argcount = parse_long_opt("diff-filter", av, &optarg))) {
3242+
options->filter = optarg;
3243+
return argcount;
3244+
}
31953245
else if (!strcmp(arg, "--abbrev"))
31963246
options->abbrev = DEFAULT_ABBREV;
31973247
else if (!prefixcmp(arg, "--abbrev=")) {
@@ -3201,20 +3251,25 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
32013251
else if (40 < options->abbrev)
32023252
options->abbrev = 40;
32033253
}
3204-
else if (!prefixcmp(arg, "--src-prefix="))
3205-
options->a_prefix = arg + 13;
3206-
else if (!prefixcmp(arg, "--dst-prefix="))
3207-
options->b_prefix = arg + 13;
3254+
else if ((argcount = parse_long_opt("src-prefix", av, &optarg))) {
3255+
options->a_prefix = optarg;
3256+
return argcount;
3257+
}
3258+
else if ((argcount = parse_long_opt("dst-prefix", av, &optarg))) {
3259+
options->b_prefix = optarg;
3260+
return argcount;
3261+
}
32083262
else if (!strcmp(arg, "--no-prefix"))
32093263
options->a_prefix = options->b_prefix = "";
32103264
else if (opt_arg(arg, '\0', "inter-hunk-context",
32113265
&options->interhunkcontext))
32123266
;
3213-
else if (!prefixcmp(arg, "--output=")) {
3214-
options->file = fopen(arg + strlen("--output="), "w");
3267+
else if ((argcount = parse_long_opt("output", av, &optarg))) {
3268+
options->file = fopen(optarg, "w");
32153269
if (!options->file)
32163270
die_errno("Could not open '%s'", arg + strlen("--output="));
32173271
options->close_file = 1;
3272+
return argcount;
32183273
} else
32193274
return 0;
32203275
return 1;

diff.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ extern void diff_unmerge(struct diff_options *,
214214
#define DIFF_SETUP_USE_CACHE 2
215215
#define DIFF_SETUP_USE_SIZE_CACHE 4
216216

217+
/*
218+
* Poor man's alternative to parse-option, to allow both sticked form
219+
* (--option=value) and separate form (--option value).
220+
*/
221+
extern int parse_long_opt(const char *opt, const char **argv,
222+
const char **optarg);
223+
217224
extern int git_diff_basic_config(const char *var, const char *value, void *cb);
218225
extern int git_diff_ui_config(const char *var, const char *value, void *cb);
219226
extern int diff_use_color_default;

t/t4013-diff-various.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ log -p --first-parent master
208208
log -m -p --first-parent master
209209
log -m -p master
210210
log -SF master
211+
log -S F master
211212
log -SF -p master
212213
log --decorate --all
213214
log --decorate=full --all
@@ -282,4 +283,8 @@ diff master master^ side
282283
diff --dirstat master~1 master~2
283284
EOF
284285

286+
test_expect_success 'log -S requires an argument' '
287+
test_must_fail git log -S
288+
'
289+
285290
test_done

t/t4013/diff.log_-S_F_master

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
$ git log -S F master
2+
commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
3+
Author: A U Thor <author@example.com>
4+
Date: Mon Jun 26 00:02:00 2006 +0000
5+
6+
Third
7+
$

t/t4202-log.sh

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,11 @@ test_expect_success 'oneline' '
100100

101101
test_expect_success 'diff-filter=A' '
102102
103-
actual=$(git log --pretty="format:%s" --diff-filter=A HEAD) &&
104-
expect=$(echo fifth ; echo fourth ; echo third ; echo initial) &&
105-
test "$actual" = "$expect" || {
106-
echo Oops
107-
echo "Actual: $actual"
108-
false
109-
}
103+
git log --pretty="format:%s" --diff-filter=A HEAD > actual &&
104+
git log --pretty="format:%s" --diff-filter A HEAD > actual-separate &&
105+
printf "fifth\nfourth\nthird\ninitial" > expect &&
106+
test_cmp expect actual &&
107+
test_cmp expect actual-separate
110108
111109
'
112110

0 commit comments

Comments
 (0)