Skip to content

Commit 6689a6e

Browse files
peffgitster
authored andcommitted
diff-highlight: fetch all config with one process
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 d1f33c7 | 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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent bd958e9 commit 6689a6e

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

contrib/diff-highlight/DiffHighlight.pm

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,21 @@ sub highlight_stdin {
131131
# of it being used in other settings. Let's handle our own
132132
# fallback, which means we will work even if git can't be run.
133133
sub color_config {
134+
our $cached_config;
134135
my ($key, $default) = @_;
135-
my $s = `git config --get-color $key 2>$NULL`;
136-
return length($s) ? $s : $default;
136+
137+
if (!defined $cached_config) {
138+
$cached_config = {};
139+
my $data = `git config --type=color --get-regexp '^color\.diff-highlight\.' 2>$NULL`;
140+
for my $line (split /\n/, $data) {
141+
my ($key, $color) = split ' ', $line, 2;
142+
$key =~ s/^color\.diff-highlight\.// or next;
143+
$cached_config->{$key} = $color;
144+
}
145+
}
146+
147+
my $s = $cached_config->{$key};
148+
return defined($s) ? $s : $default;
137149
}
138150

139151
sub show_hunk {
@@ -172,16 +184,16 @@ sub load_color_config {
172184
# always be set if you want highlighting to do anything.
173185
if (!defined $OLD_HIGHLIGHT[1]) {
174186
@OLD_HIGHLIGHT = (
175-
color_config('color.diff-highlight.oldnormal'),
176-
color_config('color.diff-highlight.oldhighlight', "\x1b[7m"),
177-
color_config('color.diff-highlight.oldreset', "\x1b[27m")
187+
color_config('oldnormal'),
188+
color_config('oldhighlight', "\x1b[7m"),
189+
color_config('oldreset', "\x1b[27m")
178190
);
179191
}
180192
if (!defined $NEW_HIGHLIGHT[1]) {
181193
@NEW_HIGHLIGHT = (
182-
color_config('color.diff-highlight.newnormal', $OLD_HIGHLIGHT[0]),
183-
color_config('color.diff-highlight.newhighlight', $OLD_HIGHLIGHT[1]),
184-
color_config('color.diff-highlight.newreset', $OLD_HIGHLIGHT[2])
194+
color_config('newnormal', $OLD_HIGHLIGHT[0]),
195+
color_config('newhighlight', $OLD_HIGHLIGHT[1]),
196+
color_config('newreset', $OLD_HIGHLIGHT[2])
185197
);
186198
};
187199
}

0 commit comments

Comments
 (0)