git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 4/4] diff-highlight: add maxhunksize config option

From
JLJonathan Lebon <jonathan.lebon@gmail.com>
Date
Nov 3, 2015, 02:05 UTC
Message-ID
<1446516334-27652-5-git-send-email-jonathan.lebon@gmail.com>
In-Reply-To
<1446516334-27652-1-git-send-email-jonathan.lebon@gmail.com>

As the size of the hunk gets bigger, it becomes harder to jump back and forth between the removed and added lines, and highlighting becomes less beneficial. We add a new config option called

	diff-highlight.maxhunksize

which controls the maximum size of the hunk allowed for which highlighting is still performed. The default value is set to 20.

Signed-off-by: Jonathan Lebon <jonathan.lebon@gmail.com>
---
 contrib/diff-highlight/README         | 20 ++++++++++++++++++++
 contrib/diff-highlight/diff-highlight | 16 ++++++++++++++++
 2 files changed, 36 insertions(+)
diff --git a/contrib/diff-highlight/README b/contrib/diff-highlight/README
index 885ff2f..ed12be1 100644
--- a/contrib/diff-highlight/README
+++ b/contrib/diff-highlight/README
@@ -89,6 +89,26 @@ newHighlight = "black #aaffaa"
 ---------------------------------------------
 
 
+Max Hunk Config
+---------------
+
+By default, diff-highlight will not do any highlighting if either the
+number of removed or added lines is greater than 20. This is because as
+the hunk gets bigger, it becomes harder to jump back and forth between
+the removed and added lines, and highlighting becomes less beneficial.
+
+You can change this default by setting the "diff-highlight.maxhunksize"
+configuration.
+
+Example:
+
+---------------------------------------------
+# Increase the maximum diff-highlight to 30
+[diff-highlight]
+maxhunksize = 30
+---------------------------------------------
+
+
 Bugs
 ----
 
diff --git a/contrib/diff-highlight/diff-highlight b/contrib/diff-highlight/diff-highlight
index 46556fc..a005146 100755
--- a/contrib/diff-highlight/diff-highlight
+++ b/contrib/diff-highlight/diff-highlight
@@ -17,6 +17,8 @@ my @NEW_HIGHLIGHT = (
 	color_config('color.diff-highlight.newreset', $OLD_HIGHLIGHT[2])
 );
 
+my $MAX_HUNK_SIZE = config('diff-highlight.maxhunksize', 20);
+
 my $RESET = "\x1b[m";
 my $COLOR = qr/\x1b\[[0-9;]*m/;
 my $BORING = qr/$COLOR|\s/;
@@ -79,6 +81,13 @@ sub color_config {
 	return length($s) ? $s : $default;
 }
 
+# Also handle our own fallback here to be independent.
+sub config {
+	my ($key, $default) = @_;
+	my $s = `git config --get $key 2>/dev/null`;
+	return length($s) ? $s : $default;
+}
+
 sub show_hunk {
 	my ($a, $b) = @_;
 
@@ -88,6 +97,13 @@ sub show_hunk {
 		return;
 	}
 
+	# Skip highlighting if the hunk gets bigger than the user configured
+	# limit.
+	if (@$a > $MAX_HUNK_SIZE || @$b > $MAX_HUNK_SIZE) {
+		print @$a, @$b;
+		return;
+	}
+
 	my @queue;
 	match_and_highlight_pairs($a, 0, scalar @$a, $b, 0, scalar @$b, \@queue);
 	print @queue;
-- 
2.6.0
Previous: Jeff KingNext: Jeff King
Message 15 of 17 in “diff-highlight: make a few improvements”
  1. 0/4 diff-highlight: make a few improvementsJonathan Lebon, Nov 3, 2015
  2. 1/4 diff-highlight: add `less -r` to cmd in READMEJonathan Lebon, Nov 3, 2015
  3. Junio C HamanoNov 3, 2015
  4. Jonathan LebonNov 3, 2015
  5. Jeff KingNov 3, 2015
  6. Junio C HamanoNov 3, 2015
  7. Jeff KingNov 3, 2015
  8. 2/4 diff-highlight: factor out prefix/suffix functionsJonathan Lebon, Nov 3, 2015
  9. Jeff KingNov 3, 2015
  10. 3/4 diff-highlight: match up lines before highlightingJonathan Lebon, Nov 3, 2015
  11. Jeff KingNov 3, 2015
  12. Jeff KingNov 3, 2015
  13. Jonathan LebonNov 17, 2015
  14. Jeff KingNov 17, 2015
  15. 4/4 diff-highlight: add maxhunksize config optionJonathan Lebon, Nov 3, 2015
  16. Jeff KingNov 3, 2015
  17. Jeff KingNov 3, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.