Skip to content

Commit 20aa7c5

Browse files
committed
Merge branch 'nd/diff-parseopt'
A brown-paper-bag bugfix to a change already in 'master'. * nd/diff-parseopt: parse-options: check empty value in OPT_INTEGER and OPT_ABBREV diff-parseopt: restore -U (no argument) behavior diff-parseopt: correct variable types that are used by parseopt
2 parents aa25c82 + f7e68a0 commit 20aa7c5

8 files changed

+107
-5
lines changed

diff.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5211,9 +5211,11 @@ static int diff_opt_unified(const struct option *opt,
52115211

52125212
BUG_ON_OPT_NEG(unset);
52135213

5214-
options->context = strtol(arg, &s, 10);
5215-
if (*s)
5216-
return error(_("%s expects a numerical value"), "--unified");
5214+
if (arg) {
5215+
options->context = strtol(arg, &s, 10);
5216+
if (*s)
5217+
return error(_("%s expects a numerical value"), "--unified");
5218+
}
52175219
enable_patch_output(&options->output_format);
52185220

52195221
return 0;
@@ -5272,7 +5274,7 @@ static void prep_parse_options(struct diff_options *options)
52725274
DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
52735275
OPT_CALLBACK_F('U', "unified", options, N_("<n>"),
52745276
N_("generate diffs with <n> lines context"),
5275-
PARSE_OPT_NONEG, diff_opt_unified),
5277+
PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_unified),
52765278
OPT_BOOL('W', "function-context", &options->flags.funccontext,
52775279
N_("generate diffs with <n> lines context")),
52785280
OPT_BIT_F(0, "raw", &options->output_format,

diff.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ struct diff_options {
169169
const char *prefix;
170170
int prefix_length;
171171
const char *stat_sep;
172-
long xdl_opts;
172+
int xdl_opts;
173173

174174
/* see Documentation/diff-options.txt */
175175
char **anchors;

parse-options-cb.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
1616
if (!arg) {
1717
v = unset ? 0 : DEFAULT_ABBREV;
1818
} else {
19+
if (!*arg)
20+
return error(_("option `%s' expects a numerical value"),
21+
opt->long_name);
1922
v = strtol(arg, (char **)&arg, 10);
2023
if (*arg)
2124
return error(_("option `%s' expects a numerical value"),

parse-options.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
195195
}
196196
if (get_arg(p, opt, flags, &arg))
197197
return -1;
198+
if (!*arg)
199+
return error(_("%s expects a numerical value"),
200+
optname(opt, flags));
198201
*(int *)opt->value = strtol(arg, (char **)&s, 10);
199202
if (*s)
200203
return error(_("%s expects a numerical value"),

t/t4013-diff-various.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ format-patch --inline --stdout initial..master^^
338338
format-patch --stdout --cover-letter -n initial..master^
339339
340340
diff --abbrev initial..side
341+
diff -U initial..side
342+
diff -U1 initial..side
341343
diff -r initial..side
342344
diff --stat initial..side
343345
diff -r --stat initial..side

t/t4013/diff.diff_-U1_initial..side

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
$ git diff -U1 initial..side
2+
diff --git a/dir/sub b/dir/sub
3+
index 35d242b..7289e35 100644
4+
--- a/dir/sub
5+
+++ b/dir/sub
6+
@@ -2 +2,3 @@ A
7+
B
8+
+1
9+
+2
10+
diff --git a/file0 b/file0
11+
index 01e79c3..f4615da 100644
12+
--- a/file0
13+
+++ b/file0
14+
@@ -3 +3,4 @@
15+
3
16+
+A
17+
+B
18+
+C
19+
diff --git a/file3 b/file3
20+
new file mode 100644
21+
index 0000000..7289e35
22+
--- /dev/null
23+
+++ b/file3
24+
@@ -0,0 +1,4 @@
25+
+A
26+
+B
27+
+1
28+
+2
29+
$

t/t4013/diff.diff_-U2_initial..side

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
$ git diff -U2 initial..side
2+
diff --git a/dir/sub b/dir/sub
3+
index 35d242b..7289e35 100644
4+
--- a/dir/sub
5+
+++ b/dir/sub
6+
@@ -1,2 +1,4 @@
7+
A
8+
B
9+
+1
10+
+2
11+
diff --git a/file0 b/file0
12+
index 01e79c3..f4615da 100644
13+
--- a/file0
14+
+++ b/file0
15+
@@ -2,2 +2,5 @@
16+
2
17+
3
18+
+A
19+
+B
20+
+C
21+
diff --git a/file3 b/file3
22+
new file mode 100644
23+
index 0000000..7289e35
24+
--- /dev/null
25+
+++ b/file3
26+
@@ -0,0 +1,4 @@
27+
+A
28+
+B
29+
+1
30+
+2
31+
$

t/t4013/diff.diff_-U_initial..side

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
$ git diff -U initial..side
2+
diff --git a/dir/sub b/dir/sub
3+
index 35d242b..7289e35 100644
4+
--- a/dir/sub
5+
+++ b/dir/sub
6+
@@ -1,2 +1,4 @@
7+
A
8+
B
9+
+1
10+
+2
11+
diff --git a/file0 b/file0
12+
index 01e79c3..f4615da 100644
13+
--- a/file0
14+
+++ b/file0
15+
@@ -1,3 +1,6 @@
16+
1
17+
2
18+
3
19+
+A
20+
+B
21+
+C
22+
diff --git a/file3 b/file3
23+
new file mode 100644
24+
index 0000000..7289e35
25+
--- /dev/null
26+
+++ b/file3
27+
@@ -0,0 +1,4 @@
28+
+A
29+
+B
30+
+1
31+
+2
32+
$

0 commit comments

Comments
 (0)