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

Re: [PATCH] Improve contrib/diff-highlight to highlight unevenly-sized hunks

From
Jeff King <peff@peff.net>
Date
Jun 19, 2015, 11:38 UTC
Message-ID
<20150619113847.GA31824@peff.net>
In-Reply-To
<20150619073455.GA29109@peff.net>
On Fri, Jun 19, 2015 at 03:34:55AM -0400, Jeff King wrote:
Show 11 quoted lines
> And here's some more bad news. If you look at the diff for this
> patch itself, it's terribly unreadable (the regular diff already is
> pretty bad, but the highlights make it much worse). There are big chunks
> where we take away 5 or 10 lines from the old code, and replace them
> with totally unrelated lines. We end up highlighting almost the entire
> thing, except for spaces and punctuation.
> 
> We might be able to solve this with a percentage heuristic similar to
> the one Patrick proposed. It's not really interesting to highlight
> unless we're doing it on probably 20% or less of the diff (where 20% is
> a number I just made up).

That turned out to be pretty easy; patch is below (on top of what I sent earlier). I set the percentage at 50% based on eyeballing "git log -p" in git.git, and it seems to give good results.

So I think the big remaining issue is improved tokenizing. Maybe Patrick will want to take a stab at it.

---
diff --git a/contrib/diff-highlight/diff-highlight b/contrib/diff-highlight/diff-highlight
index 1525ccc..9454446 100755
--- a/contrib/diff-highlight/diff-highlight
+++ b/contrib/diff-highlight/diff-highlight
@@ -114,12 +114,32 @@ sub show_hunk {
 			if $bits & 2;
 	}
 
+	my $highlighted = count_highlight(@highlight_a) +
+			  count_highlight(@highlight_b);
+	my $total = length($a) + length($b);
+	my $pct = $highlighted / $total;
+
+	if ($pct > 0.5) {
+		@highlight_a = ();
+		@highlight_b = ();
+	}
+
 	# And now show the output both with the original stripped annotations,
 	# as well as our new highlights.
 	show_image($a, [merge_annotations(\@stripped_a, \@highlight_a)]);
 	show_image($b, [merge_annotations(\@stripped_b, \@highlight_b)]);
 }
 
+sub count_highlight {
+	my $total = 0;
+	while (@_) {
+		my $from = shift;
+		my $to = shift;
+		$total += $to->[0] - $from->[0];
+	}
+	return $total;
+}
+
 # Strip out any diff syntax (i.e., leading +/-), along with any ANSI color
 # codes from the pre- or post-image of a hunk. The result is a string of text
 # suitable for diffing against the other side of the hunk.
Previous: Jeff KingNext: Junio C Hamano
Message 15 of 20 in “Improve contrib/diff-highlight to highlight unevenly-sized hunks”
  1. Improve contrib/diff-highlight to highlight unevenly-sized hunksPatrick Palka, Jun 15, 2015
  2. Junio C HamanoJun 18, 2015
  3. Patrick PalkaJun 18, 2015
  4. Junio C HamanoJun 18, 2015
  5. Jeff KingJun 18, 2015
  6. Patrick PalkaJun 18, 2015
  7. Jeff KingJun 18, 2015
  8. Jeff KingJun 18, 2015
  9. Junio C HamanoJun 18, 2015
  10. Patrick PalkaJun 18, 2015
  11. Jeff KingJun 19, 2015
  12. Junio C HamanoJun 19, 2015
  13. Jeff KingJun 19, 2015
  14. Jeff KingJun 19, 2015
  15. Jeff KingJun 19, 2015
  16. Junio C HamanoJun 19, 2015
  17. Patrick PalkaJun 18, 2015
  18. Patrick PalkaJun 18, 2015
  19. Jeff KingJun 18, 2015
  20. Patrick PalkaJun 18, 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.