diff: add interhunk context config option

The --inter-hunk-context= option was added in commit 6d0e674a5754 ("diff: add option to show context between close hunks"). This patch allows configuring a default for this option. Signed-off-by: Vegard Nossum <vegard.nossum@oracle.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Vegard Nossum committed Jan 12, 2017 at 13:21 UTC c488867793dc9b273c1d35746452d44afcd3d7f5
4 files changed +42 -1
Documentation/diff-config.txt
+6
@@ -60,6 +60,12 @@ diff.context::
60 Generate diffs with <n> lines of context instead of the default
61 of 3. This value is overridden by the -U option.
62
63 +diff.interHunkContext::
64 + Show the context between diff hunks, up to the specified number
65 + of lines, thereby fusing the hunks that are close to each other.
66 + This value serves as the default for the `--inter-hunk-context`
67 + command line option.
68 +
69 diff.external::
70 If this config variable is set, diff generation is not
71 performed using the internal diff machinery, but using the
Documentation/diff-options.txt
+2
@@ -511,6 +511,8 @@ endif::git-format-patch[]
511 --inter-hunk-context=<lines>::
512 Show the context between diff hunks, up to the specified number
513 of lines, thereby fusing hunks that are close to each other.
514 + Defaults to `diff.interHunkContext` or 0 if the config option
515 + is unset.
516
517 -W::
518 --function-context::
diff.c
+8
@@ -32,6 +32,7 @@ static int diff_rename_limit_default = 400;
32 static int diff_suppress_blank_empty;
33 static int diff_use_color_default = -1;
34 static int diff_context_default = 3;
35 +static int diff_interhunk_context_default;
36 static const char *diff_word_regex_cfg;
37 static const char *external_diff_cmd_cfg;
38 static const char *diff_order_file_cfg;
@@ -239,6 +240,12 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
240 return -1;
241 return 0;
242 }
243 + if (!strcmp(var, "diff.interhunkcontext")) {
244 + diff_interhunk_context_default = git_config_int(var, value);
245 + if (diff_interhunk_context_default < 0)
246 + return -1;
247 + return 0;
248 + }
249 if (!strcmp(var, "diff.renames")) {
250 diff_detect_rename_default = git_config_rename(var, value);
251 return 0;
@@ -3362,6 +3369,7 @@ void diff_setup(struct diff_options *options)
3369 options->rename_limit = -1;
3370 options->dirstat_permille = diff_dirstat_permille_default;
3371 options->context = diff_context_default;
3372 + options->interhunkcontext = diff_interhunk_context_default;
3373 options->ws_error_highlight = ws_error_highlight_default;
3374 DIFF_OPT_SET(options, RENAME_EMPTY);
3375
t/t4032-diff-inter-hunk-context.sh
+26 -1
@@ -16,11 +16,15 @@ f() {
16 }
17
18 t() {
19 + use_config=
20 + git config --unset diff.interHunkContext
21 +
22 case $# in
23 4) hunks=$4; cmd="diff -U$3";;
24 5) hunks=$5; cmd="diff -U$3 --inter-hunk-context=$4";;
25 + 6) hunks=$5; cmd="diff -U$3"; git config diff.interHunkContext $4; use_config="(diff.interHunkContext=$4) ";;
26 esac
23 - label="$cmd, $1 common $2"
27 + label="$use_config$cmd, $1 common $2"
28 file=f$1
29 expected=expected.$file.$3.$hunks
30
@@ -89,4 +93,25 @@ t 9 lines 3 2
93 t 9 lines 3 2 2
94 t 9 lines 3 3 1
95
96 +# use diff.interHunkContext?
97 +t 1 line 0 0 2 config
98 +t 1 line 0 1 1 config
99 +t 1 line 0 2 1 config
100 +t 9 lines 3 3 1 config
101 +t 2 lines 0 0 2 config
102 +t 2 lines 0 1 2 config
103 +t 2 lines 0 2 1 config
104 +t 3 lines 1 0 2 config
105 +t 3 lines 1 1 1 config
106 +t 3 lines 1 2 1 config
107 +t 9 lines 3 2 2 config
108 +t 9 lines 3 3 1 config
109 +
110 +test_expect_success 'diff.interHunkContext invalid' '
111 + git config diff.interHunkContext asdf &&
112 + test_must_fail git diff &&
113 + git config diff.interHunkContext -1 &&
114 + test_must_fail git diff
115 +'
116 +
117 test_done