Skip to content

Instantly share code, notes, and snippets.

@skanev
Last active January 12, 2023 19:13
Show Gist options
  • Save skanev/e7f494994c5cbac30e9f to your computer and use it in GitHub Desktop.
Save skanev/e7f494994c5cbac30e9f to your computer and use it in GitHub Desktop.
Syntax Highlight in Git Diff

Hacky syntax highlighting in git diff

Normally git diff would color additions green and deletions red. This is cool, but it would be even cooler if it adds syntax highlighting to those lines. This is a git pager that does so.

It parses the diff output and picks up the SHAs of files with additions and deletions. It uses CodeRay to highlight each file and then it extracts the lines that are shown in the diff. It then uses term/ansicolor to make a gradient from the CodeRay color and the diff color (red for deletion, green for addition) and uses it to replace the original.

I tried using rugged instead of shelling out to git show – it was faster overall, but it did incur a noticeable start up time.

Check out the image below for a demo.

#!/usr/bin/env ruby
require 'coderay'
require 'term/ansicolor'
# --- Adjusting colors -------------------------------------------------------
RGB = Term::ANSIColor::RGBTriple
Attribute = Term::ANSIColor::Attribute
Color = Term::ANSIColor
GREEN = RGB.new(0, 256, 0)
RED = RGB.new(256, 0, 0)
ADJUSTMENTS = {}
GRADIENTS = {}
def adjust(text, target)
open = adjust_seq("\e[37m", target)
adjusted = text.chomp.gsub(/\e\[[0-9;]+m/) { |seq| adjust_seq(seq, target) }
"#{open}#{adjusted}"
end
def adjust_seq(seq, target)
key = [seq, target]
ADJUSTMENTS[key] ||=
begin
escapes = []
seq[/\d+(;\d+)*/].split(';').map(&:to_i).each do |number|
case number
when 0 then escapes << ["\e[0m", gradient_to(255, target)]
when 1 then escapes << "\e[1m"
when 4 then escapes << "\e[4m"
when 30..37 then escapes << gradient_to(number - 30, target)
when 40..47 then escapes << gradient_to(number - 30, target, :background)
else escapes << seq
end
end
escapes.join('')
end
end
def gradient_to(num, target, background = false)
key = [num, target, background]
GRADIENTS[key] ||=
begin
triple = triple(num)
target = triple.gradient_to(target)[8]
html = target.html
method = background ? "on_#{html}" : html
Attribute[method].apply
end
end
def triple(num)
Attribute[num].to_rgb_triple
end
# --- Parsing git diffs ------------------------------------------------------
FORMATS = {
'Gemfile' => :ruby,
'rb' => :ruby,
'c' => :c,
'h' => :c,
'cpp' => :cpp,
'cxx' => :cpp,
'hpp' => :cpp,
'clj' => :clojure,
'css' => :css,
'erb' => :erb,
'go' => :go,
'java' => :java,
'js' => :javascript,
'json' => :json,
'php' => :php,
'lua' => :lua,
'py' => :python,
'sass' => :sass,
'scss' => :scss,
'sql' => :sql,
'xml' => :xml,
'yml' => :yaml,
'yaml' => :yaml,
}
CACHED_OBJECTS = {}
def show(sha, path)
return [] if sha =~ /^0+$/
cached = CACHED_OBJECTS.delete sha
return cached if cached
format = FORMATS[path[/(\w+)$/, 1]]
code = `git show #{sha} 2> /dev/null`
code = File.read(path) if code.empty?
code = CodeRay.scan(code, format).terminal if format
CACHED_OBJECTS[sha] = code.lines
end
def process
new = nil
old = nil
remaining = nil
old_file = nil
new_file = nil
old_hash = nil
new_hash = nil
while gets
stripped = $_.gsub(/(\e\[[0-9;]*m)*/, '')
case stripped
when /^index (\w+)..(\w+)/
old_hash = $1
new_hash = $2
puts $_
# @@ -1,4 +1,4 @@
when /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/
old_start, old_length = $1.to_i - 1, ($2 || 1).to_i
new_start, new_length = $3.to_i - 1, ($4 || 1).to_i
remaining = old_length + new_length
puts $_
when /^\+\+\+ (.*)$/
new_file = $1.sub(/^[ab]\//, '')
old = show old_hash, old_file
new = show new_hash, new_file
puts $_
when /^--- (.*)$/
old_file = $1.sub(/^[ab]\//, '')
puts $_
when /^ /
puts $_
next if remaining.nil? || remaining <= 0
old_start += 1
new_start += 1
when /^\+/
puts adjust("+#{new[new_start]}", GREEN)
new_start += 1
when /^-/
puts adjust("-#{old[old_start]}", RED)
old_start += 1
else
puts $_
end
end
end
begin
process
rescue Errno::EPIPE
exit 0
end
[core]
pager = /path/to/diff-syntax-highlight.rb | less
@valo
Copy link

valo commented Oct 4, 2014

Let's move the discussion here. Here is a fork with syntax highlighting for all of the code, not only the added and removed parts: https://gist.github.com/valo/4a8f4db480c220de215d

The problem is that is becomes difficult to see the added and the remove parts if the syntax highlighting is using red and green.

@skanev, yes, probably a gradient to gray may help. I will experiment with this...

@valo
Copy link

valo commented Oct 4, 2014

Added gradient to gray and I think it is much better. The unchanged code stays shaded. It looks to me that the original color is quite changed, but I need to doublecheck on daylight conditions :)

@TysonAndre
Copy link

I also created a patched version of this based on the gist fork https://gist.github.com/skanev/0eeb943e3111a1df55fd of this fix some missing nil checks that caused exceptions. https://github.com/TysonAndre/git-diff-syntax-highlight

Other notable changes:
I patched coderay's constants to use a custom color scheme in https://github.com/TysonAndre/git-diff-syntax-highlight/blob/master/vim.rb . This may be of interest if the default color scheme is hard to read, or you want the color scheme to match other tools you use.

overrides.each do |key, value|
    CodeRay::Encoders::Terminal::TOKEN_COLORS[key] = value
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment