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

[PATCH] contrib/diff-highlight: multibyte characters diff

From
YSYoshihiro Sugi <sugi1982@gmail.com>
Date
Feb 11, 2014, 09:09 UTC
Message-ID
<1392109750-47852-1-git-send-email-sugi1982@gmail.com>
Signed-off-by: Yoshihiro Sugi <sugi1982@gmail.com>
diff-highlight split each hunks and compare them as byte sequences.
it causes problems when diff hunks include multibyte characters.
This change enable to work on such cases by decoding inputs and encoding output as utf8 string.
---
 contrib/diff-highlight/diff-highlight | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/contrib/diff-highlight/diff-highlight b/contrib/diff-highlight/diff-highlight
index c4404d4..49b4f53 100755
--- a/contrib/diff-highlight/diff-highlight
+++ b/contrib/diff-highlight/diff-highlight
@@ -2,6 +2,7 @@
 
 use warnings FATAL => 'all';
 use strict;
+use Encode qw(decode_utf8 encode_utf8);
 
 # Highlight by reversing foreground and background. You could do
 # other things like bold or underline if you prefer.
@@ -15,8 +16,9 @@ my @added;
 my $in_hunk;
 
 while (<>) {
+	$_ = decode_utf8($_);
 	if (!$in_hunk) {
-		print;
+		print encode_utf8($_);
 		$in_hunk = /^$COLOR*\@/;
 	}
 	elsif (/^$COLOR*-/) {
@@ -30,7 +32,7 @@ while (<>) {
 		@removed = ();
 		@added = ();
 
-		print;
+		print encode_utf8($_);
 		$in_hunk = /^$COLOR*[\@ ]/;
 	}
 
@@ -58,7 +60,8 @@ sub show_hunk {
 
 	# If one side is empty, then there is nothing to compare or highlight.
 	if (!@$a || !@$b) {
-		print @$a, @$b;
+		print encode_utf8($_) for @$a;
+		print encode_utf8($_) for @$b;
 		return;
 	}
 
@@ -67,17 +70,18 @@ sub show_hunk {
 	# stupid, and only handle multi-line hunks that remove and add the same
 	# number of lines.
 	if (@$a != @$b) {
-		print @$a, @$b;
+		print encode_utf8($_) for @$a;
+		print encode_utf8($_) for @$b;
 		return;
 	}
 
 	my @queue;
 	for (my $i = 0; $i < @$a; $i++) {
 		my ($rm, $add) = highlight_pair($a->[$i], $b->[$i]);
-		print $rm;
+		print encode_utf8($rm);
 		push @queue, $add;
 	}
-	print @queue;
+	print encode_utf8($_) for @queue;
 }
 
 sub highlight_pair {
-- 
1.8.5.3
Next: Junio C Hamano
Message 1 of 8 in “contrib/diff-highlight: multibyte characters diff”
  1. contrib/diff-highlight: multibyte characters diffYoshihiro Sugi, Feb 11, 2014
  2. Junio C HamanoFeb 11, 2014
  3. Jeff KingFeb 12, 2014
  4. Thomas AdamFeb 12, 2014
  5. Jeff KingFeb 12, 2014
  6. brian m. carlsonFeb 13, 2014
  7. Jeff KingFeb 13, 2014
  8. Yoshihiro SugiFeb 13, 2014

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.