How to improve git's diff highlighting?

喜你入骨 提交于 2020-04-04 08:50:51

问题


The output of git diff is optimized for code which tends to be one statement per line whereas text can (if authors like me are too lazy to use line breaks) cause diff output which is very hard to read and more of a "Where's Wally?" search than reading diff output

whereas highlighting as done on GitLab's or GitHub's web frontend shows the difference immediately

I'm aware that I'm comparing HTML and plain text (apples and oranges), however it should be possible to improve the git diff output by using different colors or adding marker characters around a change (JUnit uses [] around insertions which isn't great to read, but an example for what I mean) and it would be the first time that there's something I expect to be somewhere available in git that actually was not.


回答1:


You could use the --word-diff[=<mode>] option to make it easier to see which words have changed within a line. This is described in the man page as

Show a word diff, using the <mode> to delimit changed words. By default, words are delimited by whitespace; see --word-diff-regex below. The <mode> defaults to plain, and must be one of:

  • color – Highlight changed words using only colors. Implies --color.

  • plain – Show words as [-removed-] and {+added+}. Makes no attempts to escape the delimiters if they appear in the input, so the output may be ambiguous.

  • porcelain – Use a special line-based format intended for script consumption. Added/removed/unchanged runs are printed in the usual unified diff format, starting with a +/-/` ` character at the beginning of the line and extending to the end of the line. Newlines in the input are represented by a tilde ~ on a line of its own.

  • none – Disable word diff again.

Note that despite the name of the first mode, color is used to highlight the changed parts in all modes if enabled.




回答2:


The word-diff suggested in the other answer isn't exactly what gitlab/github do. To get the same effect, you can use diff-highlight script that is distributed with git.

First you gotta list files in your git installation, and find path to diff-highlight. It varies for different systems, and is not usually in $PATH. E.g. on my Archlinux it's /usr/share/git/diff-highlight/diff-highlight, but on Fedora it's /usr/share/git-core/contrib/diff-highlight.

Then you edit ~/.gitconfig, and add to the [pager] section following:

[pager]
    # diff-highlight is script provided by git that shows word-by-word diff
    log  = /usr/share/git/diff-highlight/diff-highlight | less
    show = /usr/share/git/diff-highlight/diff-highlight | less
    diff = /usr/share/git/diff-highlight/diff-highlight | less

Now log, diff, show commands should show difference word-by-word. Screenshot:




回答3:


Also worth mentioning is diffr. It's written in Rust and uses Myers longest common subsequence algorithm. Compared to git's diff-highlight it gives better results, see:

git's diff-highlight:

diffr:

Once installed, making use of it is similar to that of diff-highlight, i.e. edit ~/.gitconfig, and add to the [pager] section following:

[pager]
    log  = diffr | less
    show = diffr | less
    diff = diffr | less



回答4:


One solution that is purely relying on git and its contribs. This requires no additional files than what comes with git. All explanations are for Ubuntu (tested on 18.04LTS), should work similarly on other linux systems:

  • Locate the diff-highlight contrib git snippet:
find -L /usr -name diff-highlight -type f

on my system the only valid answer is:

/usr/share/doc/git/contrib/diff-highlight/diff-highlight
  • Make the corresponding perl script executable. In my case I needed to do:
sudo chmod +x /usr/share/doc/git/contrib/diff-highlight/diff-highlight
  • Update your ~/.gitconfig to get the result you want, by adding (note these are TABS, not 4 spaces):
[color "diff-highlight"]
    oldNormal = red
    oldHighlight = red 52
    newNormal = green
    newHighlight = green 22
  • Enjoy the result (note: this is only for the diff coloring + highlight, I have other things at play here too for the prompt of course :) ).



来源:https://stackoverflow.com/questions/49278577/how-to-improve-gits-diff-highlighting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!