Skip to content

Commit 5324cc6

Browse files
committed
add-patch: respect diff.context configuration
This aims to teach relevant builtins (that take in `--patch`) to respect the user's diff.context and diff.interHunkContext file configurations. Since these are both UI options and `--patch` is designed for the end user, I believe this was previously just an inconsistency, which this patch hopes to address. Signed-off-by: Leon Michalak <[email protected]>
1 parent f65182a commit 5324cc6

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

add-interactive.c

Lines changed: 14 additions & 4 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,9 @@ 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+
repo_config_get_int(r, "diff.context", &s->context);
84+
repo_config_get_int(r, "diff.interHunkContext", &s->interhunkcontext);
85+
8186
repo_config_get_bool(r, "interactive.singlekey", &s->use_single_key);
8287
if (s->use_single_key)
8388
setbuf(stdin, NULL);
@@ -1014,10 +1019,15 @@ static int run_diff(struct add_i_state *s, const struct pathspec *ps,
10141019
if (count > 0) {
10151020
struct child_process cmd = CHILD_PROCESS_INIT;
10161021

1017-
strvec_pushl(&cmd.args, "git", "diff", "-p", "--cached",
1018-
oid_to_hex(!is_initial ? &oid :
1019-
s->r->hash_algo->empty_tree),
1020-
"--", NULL);
1022+
strvec_pushl(&cmd.args, "git", "diff", "-p", "--cached", NULL);
1023+
if (s->context != -1) {
1024+
strvec_pushf(&cmd.args, "--unified=%i", s->context);
1025+
}
1026+
if (s->interhunkcontext != -1) {
1027+
strvec_pushf(&cmd.args, "--inter-hunk-context=%i", s->interhunkcontext);
1028+
}
1029+
strvec_pushl(&cmd.args, oid_to_hex(!is_initial ? &oid :
1030+
s->r->hash_algo->empty_tree), "--", NULL);
10211031
for (i = 0; i < files->items.nr; i++)
10221032
if (files->selected[i])
10231033
strvec_push(&cmd.args,

add-patch.c

Lines changed: 8 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,12 @@ 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+
}
432+
if (diff_interhunkcontext != -1) {
433+
strvec_pushf(&args, "--inter-hunk-context=%i", diff_interhunkcontext);
434+
}
427435
if (diff_algorithm)
428436
strvec_pushf(&args, "--diff-algorithm=%s", diff_algorithm);
429437
if (s->revision) {

t/t4055-diff-context.sh

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,53 @@ test_expect_success 'diff.context honored by "log"' '
4949
! grep firstline output &&
5050
git config diff.context 8 &&
5151
git log -1 -p >output &&
52-
grep "^ firstline" output
52+
grep "^ firstline" output &&
53+
git config --unset diff.context
54+
'
55+
56+
test_expect_success 'diff.context honored by "add"' '
57+
echo q | git add -p >output &&
58+
! grep firstline output &&
59+
git config diff.context 8 &&
60+
echo q | git add -p >output &&
61+
grep "^ firstline" output &&
62+
git config --unset diff.context
63+
'
64+
65+
test_expect_success 'diff.context honored by "commit"' '
66+
! echo q | git commit -p >output &&
67+
! grep firstline output &&
68+
git config diff.context 8 &&
69+
! echo q | git commit -p >output &&
70+
grep "^ firstline" output &&
71+
git config --unset diff.context
72+
'
73+
74+
test_expect_success 'diff.context honored by "checkout"' '
75+
echo q | git checkout -p >output &&
76+
! grep firstline output &&
77+
git config diff.context 8 &&
78+
echo q | git checkout -p >output &&
79+
grep "^ firstline" output &&
80+
git config --unset diff.context
81+
'
82+
83+
test_expect_success 'diff.context honored by "stash"' '
84+
! echo q | git stash -p >output &&
85+
! grep firstline output &&
86+
git config diff.context 8 &&
87+
! echo q | git stash -p >output &&
88+
grep "^ firstline" output &&
89+
git config --unset diff.context
90+
'
91+
92+
test_expect_success 'diff.context honored by "restore"' '
93+
echo q | git restore -p >output &&
94+
! grep firstline output &&
95+
git config diff.context 8 &&
96+
echo q | git restore -p >output &&
97+
grep "^ firstline" output &&
98+
git config --unset diff.context
5399
'
54100

55101
test_expect_success 'The -U option overrides diff.context' '

0 commit comments

Comments
 (0)