Skip to content

Commit 994029d

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. The user may be used to seeing their diffs with customized context size, but not in the patches "git add -p" shows them to pick from. Teach add-patch infrastructure to read these configuration variables and pass their values when spawning the underlying plumbing commands as their command line option. Signed-off-by: Leon Michalak <[email protected]>
1 parent feace2d commit 994029d

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

add-interactive.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ void init_add_i_state(struct add_i_state *s, struct repository *r)
4141
const char *value;
4242

4343
s->r = r;
44+
s->context = -1;
45+
s->interhunkcontext = -1;
4446

4547
if (repo_config_get_value(r, "color.interactive", &value))
4648
s->use_color = -1;
@@ -78,6 +80,13 @@ void init_add_i_state(struct add_i_state *s, struct repository *r)
7880
repo_config_get_string(r, "diff.algorithm",
7981
&s->interactive_diff_algorithm);
8082

83+
if (!repo_config_get_int(r, "diff.context", &s->context))
84+
if (s->context < 0)
85+
die(_("%s cannot be negative"), "diff.context");
86+
if (!repo_config_get_int(r, "diff.interHunkContext", &s->interhunkcontext))
87+
if (s->interhunkcontext < 0)
88+
die(_("%s cannot be negative"), "diff.interHunkContext");
89+
8190
repo_config_get_bool(r, "interactive.singlekey", &s->use_single_key);
8291
if (s->use_single_key)
8392
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,6 @@ static int normalize_marker(const char *p)
414414
static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
415415
{
416416
struct strvec args = STRVEC_INIT;
417-
const char *diff_algorithm = s->s.interactive_diff_algorithm;
418417
struct strbuf *plain = &s->plain, *colored = NULL;
419418
struct child_process cp = CHILD_PROCESS_INIT;
420419
char *p, *pend, *colored_p = NULL, *colored_pend = NULL, marker = '\0';
@@ -424,8 +423,12 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
424423
int res;
425424

426425
strvec_pushv(&args, s->mode->diff_cmd);
427-
if (diff_algorithm)
428-
strvec_pushf(&args, "--diff-algorithm=%s", diff_algorithm);
426+
if (s->s.context != -1)
427+
strvec_pushf(&args, "--unified=%i", s->s.context);
428+
if (s->s.interhunkcontext != -1)
429+
strvec_pushf(&args, "--inter-hunk-context=%i", s->s.interhunkcontext);
430+
if (s->s.interactive_diff_algorithm)
431+
strvec_pushf(&args, "--diff-algorithm=%s", s->s.interactive_diff_algorithm);
429432
if (s->revision) {
430433
struct object_id oid;
431434
strvec_push(&args,

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)