Git fork

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>

authored by

Leon Michalak and committed by
Junio C Hamano
2b0a2db2 97b99a9e

+38 -3
+9
add-interactive.c
··· 41 41 const char *value; 42 42 43 43 s->r = r; 44 + s->context = -1; 45 + s->interhunkcontext = -1; 44 46 45 47 if (repo_config_get_value(r, "color.interactive", &value)) 46 48 s->use_color = -1; ··· 77 79 FREE_AND_NULL(s->interactive_diff_algorithm); 78 80 repo_config_get_string(r, "diff.algorithm", 79 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"); 80 89 81 90 repo_config_get_bool(r, "interactive.singlekey", &s->use_single_key); 82 91 if (s->use_single_key)
+1
add-interactive.h
··· 18 18 19 19 int use_single_key; 20 20 char *interactive_diff_filter, *interactive_diff_algorithm; 21 + int context, interhunkcontext; 21 22 }; 22 23 23 24 void init_add_i_state(struct add_i_state *s, struct repository *r);
+6 -3
add-patch.c
··· 414 414 static int parse_diff(struct add_p_state *s, const struct pathspec *ps) 415 415 { 416 416 struct strvec args = STRVEC_INIT; 417 - const char *diff_algorithm = s->s.interactive_diff_algorithm; 418 417 struct strbuf *plain = &s->plain, *colored = NULL; 419 418 struct child_process cp = CHILD_PROCESS_INIT; 420 419 char *p, *pend, *colored_p = NULL, *colored_pend = NULL, marker = '\0'; ··· 424 423 int res; 425 424 426 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); 429 432 if (s->revision) { 430 433 struct object_id oid; 431 434 strvec_push(&args,
+22
t/t3701-add-interactive.sh
··· 1230 1230 test_cmp expect actual 1231 1231 ' 1232 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 + 1233 1255 test_done