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 <leonmichalak6@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Leon Michalak committed
Jul 29, 2025 at 07:01 UTC
2b0a2db2c0bf4870592656e8f50876957db8660c
4 files changed
+38
-3
add-interactive.c
+9
@@ -41,6 +41,8 @@ void init_add_i_state(struct add_i_state *s, struct repository *r)
41
const char *value;
42
43
s->r = r;
44
+ s->context = -1;
45
+ s->interhunkcontext = -1;
46
47
if (repo_config_get_value(r, "color.interactive", &value))
48
s->use_color = -1;
@@ -78,6 +80,13 @@ void init_add_i_state(struct add_i_state *s, struct repository *r)
80
repo_config_get_string(r, "diff.algorithm",
81
&s->interactive_diff_algorithm);
82
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
+
90
repo_config_get_bool(r, "interactive.singlekey", &s->use_single_key);
91
if (s->use_single_key)
92
setbuf(stdin, NULL);
add-interactive.h
+1
@@ -18,6 +18,7 @@ struct add_i_state {
18
19
int use_single_key;
20
char *interactive_diff_filter, *interactive_diff_algorithm;
21
+ int context, interhunkcontext;
22
};
23
24
void init_add_i_state(struct add_i_state *s, struct repository *r);
add-patch.c
+6
-3
@@ -414,7 +414,6 @@ static int normalize_marker(const char *p)
414
static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
415
{
416
struct strvec args = STRVEC_INIT;
417
- const char *diff_algorithm = s->s.interactive_diff_algorithm;
417
struct strbuf *plain = &s->plain, *colored = NULL;
418
struct child_process cp = CHILD_PROCESS_INIT;
419
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)
423
int res;
424
425
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);
432
if (s->revision) {
433
struct object_id oid;
434
strvec_push(&args,
t/t3701-add-interactive.sh
+22
@@ -1230,4 +1230,26 @@ test_expect_success 'hunk splitting works with diff.suppressBlankEmpty' '
1230
test_cmp expect actual
1231
'
1232
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
+
1255
test_done