Skip to content

Commit f16d3de

Browse files
committed
add-patch: respect diff.context configuration
Various builtins that use add-patch infrastructure do not respect the user's diff.context and diff.interHunkContext file configurations. This patch fixes this inconsistency. Signed-off-by: Leon Michalak <leonmichalak6@gmail.com>
1 parent 75424cb commit f16d3de

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

add-interactive.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ static void init_color(struct repository *r, struct add_i_state *s,
3939
void init_add_i_state(struct add_i_state *s, struct repository *r)
4040
{
4141
const char *value;
42+
int context;
43+
int interhunkcontext;
4244

4345
s->r = r;
46+
s->context = -1;
47+
s->interhunkcontext = -1;
4448

4549
if (repo_config_get_value(r, "color.interactive", &value))
4650
s->use_color = -1;
@@ -78,6 +82,19 @@ void init_add_i_state(struct add_i_state *s, struct repository *r)
7882
repo_config_get_string(r, "diff.algorithm",
7983
&s->interactive_diff_algorithm);
8084

85+
if (!repo_config_get_int(r, "diff.context", &context)) {
86+
if (context < 0)
87+
die(_("%s cannot be negative"), "diff.context");
88+
else
89+
s->context = context;
90+
};
91+
if (!repo_config_get_int(r, "diff.interHunkContext", &interhunkcontext)) {
92+
if (interhunkcontext < 0)
93+
die(_("%s cannot be negative"), "diff.interHunkContext");
94+
else
95+
s->interhunkcontext = interhunkcontext;
96+
};
97+
8198
repo_config_get_bool(r, "interactive.singlekey", &s->use_single_key);
8299
if (s->use_single_key)
83100
setbuf(stdin, NULL);

add-interactive.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct add_i_state {
1818

1919
int use_single_key;
2020
char *interactive_diff_filter, *interactive_diff_algorithm;
21+
int context, interhunkcontext;
2122
};
2223

2324
void init_add_i_state(struct add_i_state *s, struct repository *r);

add-patch.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
415415
{
416416
struct strvec args = STRVEC_INIT;
417417
const char *diff_algorithm = s->s.interactive_diff_algorithm;
418+
int diff_context = s->s.context;
419+
int diff_interhunkcontext = s->s.interhunkcontext;
418420
struct strbuf *plain = &s->plain, *colored = NULL;
419421
struct child_process cp = CHILD_PROCESS_INIT;
420422
char *p, *pend, *colored_p = NULL, *colored_pend = NULL, marker = '\0';
@@ -424,6 +426,10 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
424426
int res;
425427

426428
strvec_pushv(&args, s->mode->diff_cmd);
429+
if (diff_context != -1)
430+
strvec_pushf(&args, "--unified=%i", diff_context);
431+
if (diff_interhunkcontext != -1)
432+
strvec_pushf(&args, "--inter-hunk-context=%i", diff_interhunkcontext);
427433
if (diff_algorithm)
428434
strvec_pushf(&args, "--diff-algorithm=%s", diff_algorithm);
429435
if (s->revision) {

t/t4055-diff-context.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,46 @@ test_expect_success 'diff.context honored by "log"' '
5252
test_grep "^ firstline" output
5353
'
5454

55+
test_expect_success 'diff.context honored by "add"' '
56+
git add -p >output &&
57+
test_grep ! firstline output &&
58+
test_config diff.context 8 &&
59+
git log -1 -p >output &&
60+
test_grep "^ firstline" output
61+
'
62+
63+
test_expect_success 'diff.context honored by "commit"' '
64+
! git commit -p >output &&
65+
test_grep ! firstline output &&
66+
test_config diff.context 8 &&
67+
! git commit -p >output &&
68+
test_grep "^ firstline" output
69+
'
70+
71+
test_expect_success 'diff.context honored by "checkout"' '
72+
git checkout -p >output &&
73+
test_grep ! firstline output &&
74+
test_config diff.context 8 &&
75+
git checkout -p >output &&
76+
test_grep "^ firstline" output
77+
'
78+
79+
test_expect_success 'diff.context honored by "stash"' '
80+
! git stash -p >output &&
81+
test_grep ! firstline output &&
82+
test_config diff.context 8 &&
83+
! git stash -p >output &&
84+
test_grep "^ firstline" output
85+
'
86+
87+
test_expect_success 'diff.context honored by "restore"' '
88+
git restore -p >output &&
89+
test_grep ! firstline output &&
90+
test_config diff.context 8 &&
91+
git restore -p >output &&
92+
test_grep "^ firstline" output
93+
'
94+
5595
test_expect_success 'The -U option overrides diff.context' '
5696
test_config diff.context 8 &&
5797
git log -U4 -1 >output &&
@@ -82,6 +122,36 @@ test_expect_success 'negative integer config parsing' '
82122
test_grep "bad config variable" output
83123
'
84124

125+
test_expect_success 'negative integer config parsing by "add"' '
126+
test_config diff.context -1 &&
127+
test_must_fail git add -p 2>output &&
128+
test_grep "diff.context cannot be negative" output
129+
'
130+
131+
test_expect_success 'negative integer config parsing by "commit"' '
132+
test_config diff.context -1 &&
133+
test_must_fail git commit -p 2>output &&
134+
test_grep "bad config variable" output
135+
'
136+
137+
test_expect_success 'negative integer config parsing by "checkout"' '
138+
test_config diff.context -1 &&
139+
test_must_fail git checkout -p 2>output &&
140+
test_grep "diff.context cannot be negative" output
141+
'
142+
143+
test_expect_success 'negative integer config parsing by "stash"' '
144+
test_config diff.context -1 &&
145+
test_must_fail git stash -p 2>output &&
146+
test_grep "diff.context cannot be negative" output
147+
'
148+
149+
test_expect_success 'negative integer config parsing by "restore"' '
150+
test_config diff.context -1 &&
151+
test_must_fail git restore -p 2>output &&
152+
test_grep "diff.context cannot be negative" output
153+
'
154+
85155
test_expect_success '-U0 is valid, so is diff.context=0' '
86156
test_config diff.context 0 &&
87157
git diff >output &&

0 commit comments

Comments
 (0)