Skip to content

Commit 890b68b

Browse files
dschogitster
authored andcommitted
add -p: prefer color.diff.context over color.diff.plain
Git's diff machinery allows users to override the colors to use in diffs, even the plain-colored context lines. As of 8dbf3eb (diff.h: rename DIFF_PLAIN color slot to DIFF_CONTEXT, 2015-05-27), the preferred name of the config setting is `color.diff.context`, although Git still allows `color.diff.plain`. In the context of `git add -p`, this logic is a bit hard to replicate: `git_diff_basic_config()` reads all config values sequentially and if it sees _any_ `color.diff.context` or `color.diff.plain`, it accepts the new color. The Perl version of `git add -p` needs to go through `git config --get-color`, though, which allows only one key to be specified. The same goes for the built-in version of `git add -p`, which has to go through `repo_config_get_value()`. The best we can do here is to look for `.context` and if none is found, fall back to looking for `.plain`, and if still not found, fall back to the hard-coded default (which in this case is simply the empty string, as context lines are typically rendered without colored). This still leads to inconsistencies when both config names are used: the initial diff will be colored by the diff machinery. Once edited by a user, a hunk has to be re-colored by `git add -p`, though, which would then use the other setting to color the context lines. In practice, this is not _all_ that bad. The `git config` manual says this in the `color.diff.<slot>`: `context` (context text - `plain` is a historical synonym) We should therefore assume that users use either one or the other, but not both names. Besides, it is relatively uncommon to look at a hunk after editing it because it is immediately staged by default. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0cb8939 commit 890b68b

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

add-interactive.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ void init_add_i_state(struct add_i_state *s, struct repository *r)
4949

5050
init_color(r, s, "diff.frag", s->fraginfo_color,
5151
diff_get_color(s->use_color, DIFF_FRAGINFO));
52-
init_color(r, s, "diff.context", s->context_color,
53-
diff_get_color(s->use_color, DIFF_CONTEXT));
52+
init_color(r, s, "diff.context", s->context_color, "fall back");
53+
if (!strcmp(s->context_color, "fall back"))
54+
init_color(r, s, "diff.plain", s->context_color,
55+
diff_get_color(s->use_color, DIFF_CONTEXT));
5456
init_color(r, s, "diff.old", s->file_old_color,
5557
diff_get_color(s->use_color, DIFF_FILE_OLD));
5658
init_color(r, s, "diff.new", s->file_new_color,

git-add--interactive.perl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
$diff_use_color ? (
3131
$repo->get_color('color.diff.frag', 'cyan'),
3232
) : ();
33-
my ($diff_plain_color) =
33+
my ($diff_context_color) =
3434
$diff_use_color ? (
35-
$repo->get_color('color.diff.plain', ''),
35+
$repo->get_color($repo->config('color.diff.context') ? 'color.diff.context' : 'color.diff.plain', ''),
3636
) : ();
3737
my ($diff_old_color) =
3838
$diff_use_color ? (
@@ -1046,7 +1046,7 @@ sub color_diff {
10461046
colored((/^@/ ? $fraginfo_color :
10471047
/^\+/ ? $diff_new_color :
10481048
/^-/ ? $diff_old_color :
1049-
$diff_plain_color),
1049+
$diff_context_color),
10501050
$_);
10511051
} @_;
10521052
}

0 commit comments

Comments
 (0)