语法突出显示的差异

时间:2016-05-31 05:43:20

标签: git

如何使用代码语法高亮显示终端中的Git显示差异,就像GitHub所做的那样?我的意思不仅是添加绿色和删除红色,还有代码语法高亮。

4 个答案:

答案 0 :(得分:5)

可以编写一个git diff驱动程序,通过Pygments' pygmentize工具。

首先,在pygmentize中使用您首选的pygmentize设置(格式化程序/样式/过滤器)定义~/bin/pygmentize-term包装器:

#!/bin/sh
exec pygmentize -f terminal256 -O bg=dark,style=trac "$@"

然后,定义一个通用的" diff,语法高亮显示" ~/bin/hldiff中的程序:

#!/bin/bash
set -euo pipefail

# Diff two files with syntax highlighting using pygmentize.

# pygmentize-term should be a pygmentize wrapper with your preferred
# pygmentize settings (formatter / style / filters)

function prepare() {
    local fn=$1

    # Work around https://bitbucket.org/birkenfeld/pygments-main/issues/1437
    if [[ "$fn" == /dev/null ]]
    then
        return
    fi

    local lexer
    lexer=$(pygmentize -N "$fn")
    #printf "Detected lexer of %q as %q\n" "$fn" "$lexer" 1>&2

    if [[ "$lexer" == text ]]
    then
        expand "$fn" | pygmentize-term -g
    else
        expand "$fn" | pygmentize-term -l "$lexer"
    fi
}

# Use colordiff (instead of diff --color=always) solely because it
# produces consistent formatting on a per-line basis, saving us from
# having to keep track of state across lines
diff=(colordiff --color=yes)

for arg in "$@"
do
    if [[ $arg == -* ]]
    then
        diff+=("$arg")
    else
        exec {fd}< <(prepare "$arg")
        diff+=(/dev/fd/$fd)
    fi
done

sed=(
    sed
    # replace "all attributes off" (used by Pygmentize to turn off
    # bold) with "normal intensity"
    -e 's/\x1b\[00m/\x1b[22m/g'

    # Replace colordiff's foreground colors with some dark background
    # colors. You can customize them here.
    -e 's/^\x1b\[0;36m/\x1b[0;36;48;5;23m/' # cyan
    -e 's/^\x1b\[0;31m/\x1b[0;31;48;5;52m/' # red
    -e 's/^\x1b\[0;32m/\x1b[0;32;48;5;22m/' # green

    # Extend background color across the entire terminal window width
    -e 's/\x1b\[0;0m$/\x1b\[K\x1b[0m/'
)

"${diff[@]}" | "${sed[@]}"

可以使用上面的代码来代替diff来获取带有语法高亮的两个文件的差异。

要让git使用它,请在~/bin/git-hldiff-driver中为上面的内容定义一个git diff驱动程序包装器:

#!/bin/bash
set -euo pipefail

# Program suitable for GIT_EXTERNAL_DIFF, which will syntax-highlight differences.

hldiff -u "$2" "$5" || true

现在可以将GIT_EXTERNAL_DIFF设置为上面的包装器脚本,让git显示带语法高亮的差异:

$ GIT_EXTERNAL_DIFF=~/bin/git-hldiff-driver git diff

要让git show使用它,您需要指定--ext-diff开关:

$ GIT_EXTERNAL_DIFF=~/bin/git-hldiff-driver git show --ext-diff

请注意,默认情况下git show会通过寻呼机(less)发送其输出,这会导致背景颜色无法正常显示,因此还要添加--no-pager以防止这一点。

答案 1 :(得分:3)

git不能直接使用代码语法突出显示 可用的内容总结在&#34; Vive la git diff! &#34;中,其中包含以下实用程序:

$ git diff ‐‐color-words
# or
$ git diff | ~/src/git/contrib/diff-hightlight/diff-highlight

It was the best of times,
-it was the **blu**rst of times.
+it was the **wo**rst of times.

**xx**部分实际上是彩色的)

但这些亮点并不取决于语言本身 像git diff-so-fancy这样更复杂的扩展仍然是为了解决差异,而不是代码语法。

poke提到in the comments差异工具semanticmerge.com,它更了解被分散文件的语言:请参阅configuration with git

答案 2 :(得分:1)

我最近编写了一个用于执行此操作的工具:https://github.com/dandavison/delta

答案 3 :(得分:0)

vim 中启用语法高亮,然后通过 vim less 宏管道 git diff,例如,

git diff | /usr/share/vim/vim74/macros/less.sh

使用 alias less='/usr/share/vim/vim74/macros/less.sh' 可以更轻松地完成此操作,以便您可以执行 git diff | less 并突出显示。