Skip to content

Commit 1ec8a13

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. This is because the plumbing commands used by "git add -p" to generate the diff do not read those config settings. Fix this by reading the config before generating the patch and passing it along to the diff command with the "-U" and "--inter-hunk-context" command-line options. Signed-off-by: Leon Michalak <[email protected]>
1 parent e5c40d3 commit 1ec8a13

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-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/t3701-add-interactive.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,4 +1230,26 @@ test_expect_success 'hunk splitting works with diff.suppressBlankEmpty' '
12301230
test_cmp expect actual
12311231
'
12321232

1233+
test_expect_success 'add -p respects diff.context' '
1234+
test_write_lines a b c d e f g h i j k l m >file &&
1235+
git add file &&
1236+
test_write_lines a b c d e f G h i j k l m >file &&
1237+
echo y | git -c diff.context=5 add -p >actual &&
1238+
test_grep "@@ -2,11 +2,11 @@" actual
1239+
'
1240+
1241+
test_expect_success 'add -p respects diff.interHunkContext' '
1242+
test_write_lines a b c d e f g h i j k l m n o p q r s >file &&
1243+
git add file &&
1244+
test_write_lines a b c d E f g i i j k l m N o p q r s >file &&
1245+
echo y | git -c diff.interhunkcontext=2 add -p >actual &&
1246+
test_grep "@@ -2,16 +2,16 @@" actual
1247+
'
1248+
1249+
test_expect_success 'add -p rejects negative diff.context' '
1250+
test_config diff.context -1 &&
1251+
test_must_fail git add -p 2>output &&
1252+
test_grep "diff.context cannot be negative" output
1253+
'
1254+
12331255
test_done

0 commit comments

Comments
 (0)