Skip to content

Commit

Permalink
far more attractive git diffs. word-highlight and simplified header. h…
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Sep 21, 2015
1 parent 3a90262 commit 6743b90
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 0 additions & 1 deletion .aliases
Expand Up @@ -58,7 +58,6 @@ alias undopush="git push -f origin HEAD^:master"
alias gr='[ ! -z `git rev-parse --show-cdup` ] && cd `git rev-parse --show-cdup || pwd`'



# IP addresses
alias ip="dig +short myip.opendns.com @resolver1.opendns.com"
alias localip="ipconfig getifaddr en1"
Expand Down
2 changes: 2 additions & 0 deletions .bash_profile
Expand Up @@ -13,6 +13,8 @@ case $- in
esac


ln -sf "$(brew --prefix)/share/git-core/contrib/diff-highlight/diff-highlight" ~/bin/diff-highlight

This comment has been minimized.

Copy link
@kevinburke

kevinburke Sep 27, 2015

FWIW, on my machine brew --prefix takes about 50ms per run - you can speed things up slightly by calling this once and then caching the result. See for example https://kev.inburke.com/kevin/profiling-zsh-startup-time/.

This comment has been minimized.

Copy link
@paulirish

paulirish Mar 22, 2016

Author Owner

old thread, but.. just wanted to thank you for commenting here. I'd read your zsh profiling post a few times and really appreciated that you wrote it up. :D


# generic colouriser
GRC=`which grc`
if [ "$TERM" != dumb ] && [ -n "$GRC" ]
Expand Down
16 changes: 16 additions & 0 deletions .functions
Expand Up @@ -55,6 +55,22 @@ function whois() {
/usr/bin/whois -h whois.internic.net $domain | sed '/NOTICE:/q'
}

function strip_diff_leading_symbols(){
color_code_regex="(\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K])"

# simplify the unified patch diff header
sed -r "s/^($color_code_regex)diff --git .*$//g" | \
sed -r "s/^($color_code_regex)index .*$/\n\1$(rule)/g" | \
sed -r "s/^($color_code_regex)\+\+\+(.*)$/\1+++\5\n\1$(rule)\x1B\[m/g" |\

# actually strips the leading symbols
sed -r "s/^($color_code_regex)[\+\-]/\1 /g"
}

## Print a horizontal rule
rule () {
printf "%$(tput cols)s\n"|tr " " "─"}}

This comment has been minimized.

Copy link
@iroller

iroller Sep 27, 2015

Is "}}" at the end a typo?

}


# Extract archives - use: extract <file>
Expand Down
8 changes: 8 additions & 0 deletions .gitconfig
Expand Up @@ -31,6 +31,8 @@
[core]
excludesfile = ~/.gitignore
attributesfile = ~/.gitattributes
# insanely beautiful diffs
pager = bash -lc 'diff-highlight | strip_diff_leading_symbols | less -r' -
[color "branch"]
current = yellow reverse
local = yellow
Expand All @@ -48,6 +50,12 @@
tool = opendiff


[color "diff-highlight"]
oldNormal = "red bold"
oldHighlight = "red bold 52"
newNormal = "green bold"
newHighlight = "green bold 22"

# rewrites
[url "git@github.com:"]
insteadOf = "gh:"
Expand Down

28 comments on commit 6743b90

@paulirish
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty git diff view

Screenshot first, details after. (And dotfiles config above)
image

Deets

  • Learned about diff-highlight and the pager hookup here: http://theunixtoolbox.com/git-diff-highlight/
  • It's in the homebrew git package by default, but you have to get it in your PATH somehow. (I'm using a symlink)
  • Custom colors like what you'd see in Github.
  • Diff header simplified format, thanks to some hacky regexes.
  • No + or - at the beginning of each line, to make for easier copypaste action.
  • And yeah, I slightly changed my iterm colors in the screenshot. ;)

@arthurvr
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woo, that looks good 😻

@UltCombo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paulirish Looks like your screenshot is missing the deleted line here:

Is this intentional?

@LucasArruda
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@UltCombo I saw that too. I wonder if it's an error or if it's due to the subtle change. @paulirish ?

@gerzenstl
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@UltCombo it seems that is a changed line, not a deleted one. On that line changed the GlassPane() arguments.

@UltCombo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gerzenstl yes, but take a look at how the other changed lines are handled in the screenshot. There's a pink line for the old line and darker pink highlighting the changed (removed) characters.

@chibicode
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to install gnu-sed from homebrew to get this to work. For zsh users, change bash call to zsh and add strip_diff_leading_symbols, etc to .zshenv (see here).

@mhartington
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying this with zsh, getting zsh:1: exec format error: diff-highlight, any ideas?

@epd
Copy link

@epd epd commented on 6743b90 Sep 27, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - if you're getting issues on OS X with sed complaining about the -r flag, just replace it with -E instead!

@iroller
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're installing gnu-sed be sure to change "sed" to "gsed" in strip_diff_leading_symbols.

Also there's a downside in removing "+" and "-" at the beginning of the line. If you copy&paste it into gist it won't highlight properly.

@zhoule
Copy link

@zhoule zhoule commented on 6743b90 Sep 28, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rpereira
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paulirish Is it possible to use this git diff view on vim when writing a commit message?

@flahertyb
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, on OS X I had to install gnu-sed, add it to path, and use sed -E. Thanks for this @paulirish!

@devinivy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needed a lot of massaging to get working on OSX Yosemite for me. Once I got diff-highlight in my path,

function strip_diff_leading_symbols(){
    color_code_regex=$'(\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K])'

        # simplify the unified patch diff header
        sed -E "s/^($color_code_regex)diff --git .*$//g" | \
               sed -E "s/^($color_code_regex)index .*$/\
\1$(rule)/g" | \
               sed -E "s/^($color_code_regex)\+\+\+(.*)$/\1\+\+\+\5\\
\1$(rule)/g" | \

        # actually strips the leading symbols
               sed -E "s/^($color_code_regex)[\+\-]/\1 /g"
}

## Print a horizontal rule
rule () {
        printf "%$(tput cols)s\n"|tr " " ""
}

The line breaks are escaped and intentional. But they're certainly not pretty! I'm sure someone can improve this.

@thebeline
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, can someone confirm about @UltCombo's note about the missing changed line? Also, I noticed an anomaly:

image

The line seems to write-over the next line when it wraps. Odd...

@fhemberger
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some problems to get this working with zsh 5.5.1 on OS X Mavericks. I'm using @devinivy's snippet and sourced it. When I run it on a local diff e.g. cat my.diff | strip_diff_leading_symbols it works, in my ~/.gitconfig using

pager = /usr/local/share/git-core/contrib/diff-highlight/diff-highlight | strip_diff_leading_symbols | cat -

I always get diff-highlight | strip_diff_leading_symbols | cat -: strip_diff_leading_symbols: command not found. Any idea how to fix this? Without strip_diff_leading_symbols it works fine.

@kitsunde
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used this for a couple of days now and on my OSX I've noticed some inline diffs are truncated.

@paulirish
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks all! A few hacky regexes don't hold up too long, do they? :)

I have moved this into a dedicated script: https://github.com/paulirish/dotfiles/blob/master/bin/diff-so-fancy (30a6296)

I've incorporated much of @devinivy's edits. And documented that gnu-sed is mandatory and all that.

Give it a whirl and PR me if it isn't good enough. Happy for this to graduate to its own repo too, if anyone is interested in helping maintain.

@fhemberger
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paulirish Works also with OS X's own sed if you replace -r with -E in https://github.com/paulirish/dotfiles/blob/master/bin/diff-so-fancy#L59 and https://github.com/paulirish/dotfiles/blob/master/bin/diff-so-fancy#L64

also, you don't need to call it through bash in your .gitconfig:
pager = diff-highlight | diff-so-fancy | less -r works fine.

@OliverJAsh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using zsh and https://github.com/paulirish/dotfiles/blob/master/bin/diff-so-fancy.

Following the instructions and running git diff | diff-highlight | diff-so-fancy works fine, even without changing bash to zsh.

For some reason, the context line and diff header do not have their colours applied. I played around and got the context line working by changing colorize_context_line to:

colorize_context_line () {
    # extra color for @@ context line
    sed -r "s/@@(.*)@@/$dim_magenta@@\1@@$reset_color/g"
}

Not sure why this was necessary? Any ideas why my diff header doesn't have the colours applied?

@OliverJAsh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worked it out: I also had to apply:

[color "diff"]
    meta = yellow bold

I think I had to change colorize_context_line because Paul has colours set which I don't, so the regexp doesn't match.

@paulirish
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OliverJAsh good call. filed here #48

@dimitrismistriotis
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kdeldycke
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added this fancy git diff to my dotfiles: kdeldycke/dotfiles@6b384a2

Thanks @paulirish ! 👍

@hnq90
Copy link

@hnq90 hnq90 commented on 6743b90 Nov 27, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can I use diff-so-fancy in Fedora? I always get this error when run: git diff | diff-hightlight | diff-so-fancy

$ git diff | diff-highlight | diff-so-fancy
sed: -e expression #1, char 153: unterminated `s' command
sed: -e expression #1, char 139: unterminated `s' command

@hnq90
Copy link

@hnq90 hnq90 commented on 6743b90 Dec 8, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MatrixAgent Do you use custom color in your terminal? I faced this problem even on my OSX.

@hnq90
Copy link

@hnq90 hnq90 commented on 6743b90 Dec 8, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MatrixAgent I think you should use default colors for your terminal and try it again.

@stevemao
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have created a standalone repo https://github.com/stevemao/diff-so-fancy
and pushed it to npm
I can add whoever want to contribute.

Please sign in to comment.