diff: diff.context configuration gives default to -U

Introduce a configuration variable diff.context that tells Porcelain commands to use a non-default number of context lines instead of 3 (the default). With this variable, users do not have to keep repeating "git log -U8" from the command line; instead, it becomes sufficient to say "git config diff.context 8" just once. Signed-off-by: Jeff Muizelaar <jmuizelaar@mozilla.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff Muizelaar committed Sep 27, 2012 at 15:12 UTC 6468a4e5486d01e4da5ea8e8f2990523c3d18688
3 files changed +105 -1
Documentation/diff-config.txt
+4
@@ -56,6 +56,10 @@ diff.statGraphWidth::
56 Limit the width of the graph part in --stat output. If set, applies
57 to all commands generating --stat output except format-patch.
58
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 +
63 diff.external::
64 If this config variable is set, diff generation is not
65 performed using the internal diff machinery, but using the
diff.c
+8 -1
@@ -26,6 +26,7 @@ static int diff_detect_rename_default;
26 static int diff_rename_limit_default = 400;
27 static int diff_suppress_blank_empty;
28 static int diff_use_color_default = -1;
29 +static int diff_context_default = 3;
30 static const char *diff_word_regex_cfg;
31 static const char *external_diff_cmd_cfg;
32 int diff_auto_refresh_index = 1;
@@ -141,6 +142,12 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
142 diff_use_color_default = git_config_colorbool(var, value);
143 return 0;
144 }
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 + }
151 if (!strcmp(var, "diff.renames")) {
152 diff_detect_rename_default = git_config_rename(var, value);
153 return 0;
@@ -3170,7 +3177,7 @@ void diff_setup(struct diff_options *options)
3177 options->break_opt = -1;
3178 options->rename_limit = -1;
3179 options->dirstat_permille = diff_dirstat_permille_default;
3173 - options->context = 3;
3180 + options->context = diff_context_default;
3181 DIFF_OPT_SET(options, RENAME_EMPTY);
3182
3183 options->change = diff_change;
t/t4055-diff-context.sh new
+93
@@ -0,0 +1,93 @@
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 >x <<-\EOF &&
12 + firstline
13 + b
14 + c
15 + d
16 + e
17 + f
18 + preline
19 + postline
20 + i
21 + j
22 + k
23 + l
24 + m
25 + n
26 + EOF
27 + git update-index --add x &&
28 + git commit -m initial &&
29 +
30 + git cat-file blob HEAD:x |
31 + sed "/preline/a\
32 + ADDED" >x &&
33 + git update-index --add x &&
34 + git commit -m next &&
35 +
36 + git cat-file blob HEAD:x |
37 + sed s/ADDED/MODIFIED/ >x
38 +'
39 +
40 +test_expect_success 'the default number of context lines is 3' '
41 + git diff >output &&
42 + ! grep "^ d" output &&
43 + grep "^ e" output &&
44 + grep "^ j" output &&
45 + ! grep "^ k" output
46 +'
47 +
48 +test_expect_success 'diff.context honored by "log"' '
49 + git log -1 -p >output &&
50 + ! grep firstline output &&
51 + git config diff.context 8 &&
52 + git log -1 -p >output &&
53 + grep "^ firstline" output
54 +'
55 +
56 +test_expect_success 'The -U option overrides diff.context' '
57 + git config diff.context 8 &&
58 + git log -U4 -1 >output &&
59 + ! grep "^ firstline" output
60 +'
61 +
62 +test_expect_success 'diff.context honored by "diff"' '
63 + git config diff.context 8 &&
64 + git diff >output &&
65 + grep "^ firstline" output
66 +'
67 +
68 +test_expect_success 'plumbing not affected' '
69 + git config diff.context 8 &&
70 + git diff-files -p >output &&
71 + ! grep "^ firstline" output
72 +'
73 +
74 +test_expect_success 'non-integer config parsing' '
75 + git config diff.context no &&
76 + test_must_fail git diff 2>output &&
77 + test_i18ngrep "bad config value" output
78 +'
79 +
80 +test_expect_success 'negative integer config parsing' '
81 + git config diff.context -1 &&
82 + test_must_fail git diff 2>output &&
83 + test_i18ngrep "bad config file" output
84 +'
85 +
86 +test_expect_success '-U0 is valid, so is diff.context=0' '
87 + git config diff.context 0 &&
88 + git diff >output &&
89 + grep "^-ADDED" output &&
90 + grep "^+MODIFIED" output
91 +'
92 +
93 +test_done