From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Cc: Tian Yuchen <a3205153416@gmail.com>, Scott Baker <scott@perturb.org>
Subject: [PATCH v2 8/8] diff-highlight: fetch all config with one process
Date: Mon, 23 Mar 2026 02:02:18 -0400 [thread overview]
Message-ID: <20260323060218.GH10482@coredump.intra.peff.net> (raw)
In-Reply-To: <20260323060139.GA10215@coredump.intra.peff.net>
When diff-highlight was written, there was no way to fetch multiple
config keys _and_ have them interpreted as colors. So we were stuck
with either invoking git-config once for each config key, or fetching
them all and converting human-readable color names into ANSI codes
ourselves.
I chose the former, but it means that diff-highlight kicks off 6
git-config processes (even if you haven't configured anything, it has to
check each one).
But since Git 2.18.0, we can do:
git config --type=color --get-regexp=^color\.diff-highlight\.
to get all of them in one shot.
Note that any callers which pass in colors directly to the module via
@OLD_HIGHLIGHT and @NEW_HIGHLIGHT (like diff-so-fancy plans to do) are
unaffected; those colors suppress any config lookup we'd do ourselves.
You can see the effect like:
# diff-highlight suppresses git-config's stderr, so dump
# trace through descriptor 3
git show d1f33c753d | GIT_TRACE=3 diff-highlight 3>&2 >/dev/null
which drops from 6 lines down to 1.
Signed-off-by: Jeff King <peff@peff.net>
---
contrib/diff-highlight/DiffHighlight.pm | 28 ++++++++++++++++++-------
1 file changed, 20 insertions(+), 8 deletions(-)
diff --git a/contrib/diff-highlight/DiffHighlight.pm b/contrib/diff-highlight/DiffHighlight.pm
index 96369eadf9..abe457882e 100644
--- a/contrib/diff-highlight/DiffHighlight.pm
+++ b/contrib/diff-highlight/DiffHighlight.pm
@@ -131,9 +131,21 @@ sub highlight_stdin {
# of it being used in other settings. Let's handle our own
# fallback, which means we will work even if git can't be run.
sub color_config {
+ our $cached_config;
my ($key, $default) = @_;
- my $s = `git config --get-color $key 2>$NULL`;
- return length($s) ? $s : $default;
+
+ if (!defined $cached_config) {
+ $cached_config = {};
+ my $data = `git config --type=color --get-regexp '^color\.diff-highlight\.' 2>$NULL`;
+ for my $line (split /\n/, $data) {
+ my ($key, $color) = split ' ', $line, 2;
+ $key =~ s/^color\.diff-highlight\.// or next;
+ $cached_config->{$key} = $color;
+ }
+ }
+
+ my $s = $cached_config->{$key};
+ return defined($s) ? $s : $default;
}
sub show_hunk {
@@ -172,16 +184,16 @@ sub load_color_config {
# always be set if you want highlighting to do anything.
if (!defined $OLD_HIGHLIGHT[1]) {
@OLD_HIGHLIGHT = (
- color_config('color.diff-highlight.oldnormal'),
- color_config('color.diff-highlight.oldhighlight', "\x1b[7m"),
- color_config('color.diff-highlight.oldreset', "\x1b[27m")
+ color_config('oldnormal'),
+ color_config('oldhighlight', "\x1b[7m"),
+ color_config('oldreset', "\x1b[27m")
);
}
if (!defined $NEW_HIGHLIGHT[1]) {
@NEW_HIGHLIGHT = (
- color_config('color.diff-highlight.newnormal', $OLD_HIGHLIGHT[0]),
- color_config('color.diff-highlight.newhighlight', $OLD_HIGHLIGHT[1]),
- color_config('color.diff-highlight.newreset', $OLD_HIGHLIGHT[2])
+ color_config('newnormal', $OLD_HIGHLIGHT[0]),
+ color_config('newhighlight', $OLD_HIGHLIGHT[1]),
+ color_config('newreset', $OLD_HIGHLIGHT[2])
);
};
}
--
2.53.0.1051.ga14e96f895
next prev parent reply other threads:[~2026-03-23 6:02 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-20 0:41 [PATCH 0/8] some diff-highlight tweaks Jeff King
2026-03-20 0:42 ` [PATCH 1/8] diff-highlight: mention build instructions Jeff King
2026-03-20 0:42 ` [PATCH 2/8] diff-highlight: drop perl version dependency back to 5.8 Jeff King
2026-03-20 0:43 ` [PATCH 3/8] diff-highlight: check diff-highlight exit status in tests Jeff King
2026-03-20 0:43 ` [PATCH 4/8] t: add matching negative attributes to test_decode_color Jeff King
2026-03-20 0:44 ` [PATCH 5/8] diff-highlight: use test_decode_color in tests Jeff King
2026-03-22 17:24 ` Tian Yuchen
2026-03-22 20:47 ` Jeff King
2026-03-23 5:48 ` Tian Yuchen
2026-03-23 5:53 ` Jeff King
2026-03-20 0:45 ` [PATCH 6/8] diff-highlight: test color config Jeff King
2026-03-20 0:47 ` [PATCH 7/8] diff-highlight: allow module callers to pass in " Jeff King
2026-03-20 0:48 ` [PATCH 8/8] diff-highlight: fetch all config with one process Jeff King
2026-03-22 17:18 ` Tian Yuchen
2026-03-22 20:45 ` Jeff King
2026-03-23 5:39 ` Tian Yuchen
2026-03-23 5:57 ` Jeff King
2026-03-23 6:01 ` [PATCH v2 0/8] some diff-highlight tweaks Jeff King
2026-03-23 6:01 ` [PATCH v2 1/8] diff-highlight: mention build instructions Jeff King
2026-03-23 6:02 ` [PATCH v2 2/8] diff-highlight: drop perl version dependency back to 5.8 Jeff King
2026-03-23 6:02 ` [PATCH v2 3/8] diff-highlight: check diff-highlight exit status in tests Jeff King
2026-03-23 6:02 ` [PATCH v2 4/8] t: add matching negative attributes to test_decode_color Jeff King
2026-03-23 6:02 ` [PATCH v2 5/8] diff-highlight: use test_decode_color in tests Jeff King
2026-03-23 6:02 ` [PATCH v2 6/8] diff-highlight: test color config Jeff King
2026-03-23 6:02 ` [PATCH v2 7/8] diff-highlight: allow module callers to pass in " Jeff King
2026-03-23 6:02 ` Jeff King [this message]
2026-03-23 16:38 ` [PATCH v2 0/8] some diff-highlight tweaks Junio C Hamano
2026-03-24 6:50 ` Patrick Steinhardt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260323060218.GH10482@coredump.intra.peff.net \
--to=peff@peff.net \
--cc=a3205153416@gmail.com \
--cc=git@vger.kernel.org \
--cc=scott@perturb.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox