Skip to content

Commit 8de8f9f

Browse files
committed
Merge branch 'jm/diff-context-config'
Teaches a new configuration variable to "git diff" Porcelain and its friends. * jm/diff-context-config: t4055: avoid use of sed 'a' command diff: diff.context configuration gives default to -U
2 parents 55ff630 + 50fb51e commit 8de8f9f

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

Documentation/diff-config.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ diff.statGraphWidth::
5656
Limit the width of the graph part in --stat output. If set, applies
5757
to all commands generating --stat output except format-patch.
5858

59+
diff.context::
60+
Generate diffs with <n> lines of context instead of the default of
61+
3. This value is overridden by the -U option.
62+
5963
diff.external::
6064
If this config variable is set, diff generation is not
6165
performed using the internal diff machinery, but using the

diff.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ static int diff_detect_rename_default;
2626
static int diff_rename_limit_default = 400;
2727
static int diff_suppress_blank_empty;
2828
static int diff_use_color_default = -1;
29+
static int diff_context_default = 3;
2930
static const char *diff_word_regex_cfg;
3031
static const char *external_diff_cmd_cfg;
3132
int diff_auto_refresh_index = 1;
@@ -141,6 +142,12 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
141142
diff_use_color_default = git_config_colorbool(var, value);
142143
return 0;
143144
}
145+
if (!strcmp(var, "diff.context")) {
146+
diff_context_default = git_config_int(var, value);
147+
if (diff_context_default < 0)
148+
return -1;
149+
return 0;
150+
}
144151
if (!strcmp(var, "diff.renames")) {
145152
diff_detect_rename_default = git_config_rename(var, value);
146153
return 0;
@@ -3170,7 +3177,7 @@ void diff_setup(struct diff_options *options)
31703177
options->break_opt = -1;
31713178
options->rename_limit = -1;
31723179
options->dirstat_permille = diff_dirstat_permille_default;
3173-
options->context = 3;
3180+
options->context = diff_context_default;
31743181
DIFF_OPT_SET(options, RENAME_EMPTY);
31753182

31763183
options->change = diff_change;

t/t4055-diff-context.sh

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2012 Mozilla Foundation
4+
#
5+
6+
test_description='diff.context configuration'
7+
8+
. ./test-lib.sh
9+
10+
test_expect_success 'setup' '
11+
cat >template <<-\EOF &&
12+
firstline
13+
b
14+
c
15+
d
16+
e
17+
f
18+
preline
19+
TARGET
20+
postline
21+
i
22+
j
23+
k
24+
l
25+
m
26+
n
27+
EOF
28+
sed "/TARGET/d" >x <template &&
29+
git update-index --add x &&
30+
git commit -m initial &&
31+
32+
sed "s/TARGET/ADDED/" >x <template &&
33+
git update-index --add x &&
34+
git commit -m next &&
35+
36+
sed "s/TARGET/MODIFIED/" >x <template
37+
'
38+
39+
test_expect_success 'the default number of context lines is 3' '
40+
git diff >output &&
41+
! grep "^ d" output &&
42+
grep "^ e" output &&
43+
grep "^ j" output &&
44+
! grep "^ k" output
45+
'
46+
47+
test_expect_success 'diff.context honored by "log"' '
48+
git log -1 -p >output &&
49+
! grep firstline output &&
50+
git config diff.context 8 &&
51+
git log -1 -p >output &&
52+
grep "^ firstline" output
53+
'
54+
55+
test_expect_success 'The -U option overrides diff.context' '
56+
git config diff.context 8 &&
57+
git log -U4 -1 >output &&
58+
! grep "^ firstline" output
59+
'
60+
61+
test_expect_success 'diff.context honored by "diff"' '
62+
git config diff.context 8 &&
63+
git diff >output &&
64+
grep "^ firstline" output
65+
'
66+
67+
test_expect_success 'plumbing not affected' '
68+
git config diff.context 8 &&
69+
git diff-files -p >output &&
70+
! grep "^ firstline" output
71+
'
72+
73+
test_expect_success 'non-integer config parsing' '
74+
git config diff.context no &&
75+
test_must_fail git diff 2>output &&
76+
test_i18ngrep "bad config value" output
77+
'
78+
79+
test_expect_success 'negative integer config parsing' '
80+
git config diff.context -1 &&
81+
test_must_fail git diff 2>output &&
82+
test_i18ngrep "bad config file" output
83+
'
84+
85+
test_expect_success '-U0 is valid, so is diff.context=0' '
86+
git config diff.context 0 &&
87+
git diff >output &&
88+
grep "^-ADDED" output &&
89+
grep "^+MODIFIED" output
90+
'
91+
92+
test_done

0 commit comments

Comments
 (0)