Skip to content

Instantly share code, notes, and snippets.

@mendaomn
Created October 27, 2015 15:10
Show Gist options
  • Save mendaomn/46a908027aeff8135207 to your computer and use it in GitHub Desktop.
Save mendaomn/46a908027aeff8135207 to your computer and use it in GitHub Desktop.
Gitconfig - colored and highlighted diff
[user]
email = [email protected]
name = Alessandro Menduni
[alias]
diff = diff --color --color-words --abbrev
[core]
pager = bash -lc 'diff-highlight | diff-so-fancy | less -r' -
[color "diff"]
meta = yellow bold
commit = green bold
frag = magenta bold
old = red bold
new = green bold
whitespace = red reverse
#!/bin/bash
###############
# Details
#
# Screenshot: https://github.com/paulirish/dotfiles/commit/6743b907ff58#commitcomment-13349456
# Simplified diff header format
# No `+` or `-` at the beginning of each line, to make for easier copypaste action.
###############
###############
# Usage
#
# git diff | diff-highlight | diff-so-fancy
#
# Add to .gitconfig so all `git diff` uses it.
# git config --global core.pager "bash -lc 'diff-highlight | diff-so-fancy | less -r'
###############
###############
# Requirements / Install
#
# * GNU sed. On Mac, install it with Homebrew:
# brew install gnu-sed --default-names # You'll have to change below to `gsed` otherwise
# * diff-highlight. It's shipped with Git, but probably not in your $PATH
# ln -sf "$(brew --prefix)/share/git-core/contrib/diff-highlight/diff-highlight" ~/bin/diff-highlight
# * Add to your .gitconfig:
# [color "diff-highlight"]
# oldNormal = "red bold"
# oldHighlight = "red bold 52"
# newNormal = "green bold"
# newHighlight = "green bold 22"
#
###############
# TODO:
# Put on NPM.
[ $# -ge 1 -a -f "$1" ] && input="$1" || input="-"
color_code_regex=$'(\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)[m|K])?'
reset_color="\x1B\[m"
dim_magenta="\x1B\[38;05;146m"
format_diff_header () {
# simplify the unified patch diff header
sed -E "s/^($color_code_regex)diff --git .*$//g" | \
sed -E "s/^($color_code_regex)index .*$/\
\1$(print_horizontal_rule)/g" | \
sed -E "s/^($color_code_regex)\+\+\+(.*)$/\1\+\+\+\5\\
\1$(print_horizontal_rule)/g"
}
colorize_context_line () {
# extra color for @@ context line
sed -r "s/@@$reset_color $reset_color(.*$)/@@ $dim_magenta\1/g"
}
strip_leading_symbols () {
# strip the + and -
sed -r "s/^($color_code_regex)[\+\-]/\1 /g"
}
print_horizontal_rule () {
printf "%$(tput cols)s\n"|tr " " "-"
}
# run it.
cat $input | format_diff_header | colorize_context_line | strip_leading_symbols
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment