突出显示更改的行以及每个更改行中更改的字节

发布于 2024-10-22 14:15:39 字数 461 浏览 10 评论 0原文

开源项目 Trac 有一个出色的差异荧光笔 - 它突出显示更改的行以及每个更改行中更改的字节!请参阅此处此处 作为示例。

有没有办法在 bash 终端、gitvim 中使用相同的颜色突出显示(即更改行和更改字节)用于 diff 输出(补丁文件)?

Open Source project Trac has an excellent diff highlighter — it highlights changed lines and changed bytes in each changed line! See here or here for examples.

Is there way to use the same color highlight (i.e. changed lines and changed bytes too) in bash terminal, git, or vim for diff output (patch-file)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

暖心男生 2024-10-29 14:15:39

diff-highlight Perl contrib 脚本 生成的输出与 Trac 屏幕截图非常相似,因此 Trac 很可能正在使用它:

screenshot of diff-highlight in use

安装方式:

wget https://raw.githubusercontent.com/git/git/fd99e2bda0ca6a361ef03c04d6d7fdc7a9c40b78/contrib/diff-highlight/diff-highlight && chmod +x diff-highlight

将文件 diff-highlight 移动到 ~/bin/ 目录(或 $PATH 所在的任何位置),然后然后将以下内容添加到您的 ~/.gitconfig 中:

[pager]
    diff = diff-highlight | less
    log = diff-highlight | less
    show = diff-highlight | less

@cirosantilli 建议的单个复制粘贴安装:

cd ~/bin
curl -O https://raw.githubusercontent.com/git/git/fd99e2bda0ca6a361ef03c04d6d7fdc7a9c40b78/contrib/diff-highlight/diff-highlight
chmod +x diff-highlight
git config --global pager.log 'diff-highlight | less'
git config --global pager.show 'diff-highlight | less'
git config --global pager.diff 'diff-highlight | less'
git config --global interactive.diffFilter diff-highlight

The diff-highlight Perl contrib script produces output so similar to that of the Trac screenshots that it is likely that Trac is using it:

screenshot of diff-highlight in use

Install with:

wget https://raw.githubusercontent.com/git/git/fd99e2bda0ca6a361ef03c04d6d7fdc7a9c40b78/contrib/diff-highlight/diff-highlight && chmod +x diff-highlight

Move the file diff-highlight to the ~/bin/ directory (or wherever your $PATH is), and then add the following to your ~/.gitconfig:

[pager]
    diff = diff-highlight | less
    log = diff-highlight | less
    show = diff-highlight | less

Single copy paste install suggested by @cirosantilli:

cd ~/bin
curl -O https://raw.githubusercontent.com/git/git/fd99e2bda0ca6a361ef03c04d6d7fdc7a9c40b78/contrib/diff-highlight/diff-highlight
chmod +x diff-highlight
git config --global pager.log 'diff-highlight | less'
git config --global pager.show 'diff-highlight | less'
git config --global pager.diff 'diff-highlight | less'
git config --global interactive.diffFilter diff-highlight
晨曦慕雪 2024-10-29 14:15:39

使用 git diffgit log 以及其他可能的选项时,请使用选项 --word-diff=color (还有其他用于单词差异的模式顺便提一句)

While using git diff or git log and possibly others, use option --word-diff=color (there are also other modes for word diffs BTW)

戒ㄋ 2024-10-29 14:15:39

diff-so-fancy 是一个 diff-专为人眼设计的荧光笔。

它删除了前导 +/-(这对于剪切/粘贴来说很烦人),并使文件之间的部分变得清晰。

彩色 git(左)与 diff-so-fancy(右 - 注意字符级突出显示):

diff-so-fancy 输出

如果您想要< a href="https://github.com/so-fancy/diff-so-fancy" rel="noreferrer">diff-so-fancy (右侧)输出,但是不限于 git 存储库中的文件,请将以下函数添加到 .bashrc 中以在任何文件上使用它:

dsf() { git diff --no-index --color "$@" | diff-so-fancy; }

例如:

dsf original changed-file

字符级别突出显示和标准 diff< /code> 格式

如果您不喜欢 diff-so-fancy 的非标准格式,但仍希望字符级 git 突出显示,请使用 diff-highlight 它将采用 git 的输出并产生非常漂亮的标准diff-格式输出:

diff-highlight Screenshot

要从 git 默认使用它,请添加到您的 .gitconfig

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

[pager]
  diff = diff-highlight | less -FRXsu --tabs=4

: >[pager] 部分告诉 git 将其已经着色的输出通过管道传输到 diff-highlight,后者在字符级别着色,然后将输出分页到 less (如果需要),而不是仅仅使用默认的 less

diff-so-fancy is a diff-highlighter designed for human eyeballs.

It removes the leading +/- which are annoying for cut/paste and makes clear sections between files.

Coloured git (left) vs diff-so-fancy (right - note the character-level highlights):

diff-so-fancy output

If you want thediff-so-fancy (right side) output but not constrained to files in a git repository, add the following function to your .bashrc to use it on any files:

dsf() { git diff --no-index --color "$@" | diff-so-fancy; }

Eg:

dsf original changed-file

Character level highlighting and standard diff format

If you don't like the non-standard formatting of diff-so-fancy, but still want character-level git highlighting, use diff-highlight which will take git's output and produce the really pretty standard diff-format output:

diff-highlight screenshot

To use it by default from git, add to your .gitconfig:

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

[pager]
  diff = diff-highlight | less -FRXsu --tabs=4

The [pager] section tells git to pipe its already colourised output to diff-highlight which colourises at the character level, and then pages the output in less (if required), rather than just using the default less.

东京女 2024-10-29 14:15:39

自 v1.7.81 起,基于字节差异的实用程序已随官方 Git 一起分发。您只需找到它在计算机上的安装位置并启用它即可。

查找 Git 的安装位置

diff-highlight 链接到您的 bin 目录,以便您的 PATH 可以找到它

GIT_HOME='/usr/local/opt/git/'  # Use the value from the first step.
ln -s "${GIT_HOME}/share/git-core/contrib/diff-highlight/diff-highlight" \
      '/usr/local/bin/diff-highlight'

在 Git 配置中启用它

git config --global interactive.diffFilter diff-highlight # Use on interactive prompts
git config --global pager.diff "diff-highlight | less"    # Use on git diff
git config --global pager.log  "diff-highlight | less"    # Use on git log
git config --global pager.show "diff-highlight | less"    # Use on git show

1 com/git/git/commit/927a13fe87e4b2e0edabb2f8615f0851f70fbbd8#diff-49404b1873b5e4ceb57f209c3ebd7762" rel="nofollow noreferrer">v1.7.8 版本,但是 从那时起已经进行了很多更改

A utility for byte-based diffs has been distributed with official Git since v1.7.81. You just have to locate where it is installed on your machine and enable it.

Find where Git is installed

  • MacOS with Git installed via Homebrew: It's /usr/local/opt/git (later versions: /opt/homebrew/Cellar/git/VERSION)
  • Windows with Git for Windows: Run cd / && pwd -W to find the install directory.
  • Linux: Nerd. If you don't already know where Git is installed, then ll $(which git) or locate git should help.

Link diff-highlight to your bin directory so that your PATH can find it

GIT_HOME='/usr/local/opt/git/'  # Use the value from the first step.
ln -s "${GIT_HOME}/share/git-core/contrib/diff-highlight/diff-highlight" \
      '/usr/local/bin/diff-highlight'

Enable it in your Git config

git config --global interactive.diffFilter diff-highlight # Use on interactive prompts
git config --global pager.diff "diff-highlight | less"    # Use on git diff
git config --global pager.log  "diff-highlight | less"    # Use on git log
git config --global pager.show "diff-highlight | less"    # Use on git show

1 Here is the v1.7.8 version, but lots of changes have been made since then.

自我难过 2024-10-29 14:15:39

您想要的行为现在可以在 git 本身中使用(正如 naught101 在评论中指出的那样)。要启用它,您需要将寻呼机设置为

perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less

/usr/share/doc/git/contrib/diff-highlight/diff-highlight 是 Ubuntu 13.10 上荧光笔脚本的位置(我没有知道为什么它位于 doc 文件夹中)。如果您的系统上不存在,请尝试使用 locate diff-highlight 来查找它。请注意,突出显示脚本不可执行(至少在我的机器上),因此需要 perl

要始终对各种类似 diff 的命令使用荧光笔,只需将以下内容添加到您的 ~/.gitconfig 文件中:

[pager]
    log = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less
    show = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less
    diff = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less

我将其添加为新答案 naught101 的评论已被隐藏,因为设置并不完全应该是微不足道的,至少在 Ubuntu 版本上,我在 README 中有说明 不 工作。

The behaviour you want is now available in git itself (as was pointed out in a comment by naught101). To enable it you need to set your pager to

perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less

where /usr/share/doc/git/contrib/diff-highlight/diff-highlight is the location of the highlighter script on Ubuntu 13.10 (I have no idea why it's in a doc folder). If it isn't there on your system try using locate diff-highlight to find it. Note that the highlighting script is not executable (at least on my machine), hence the requirement for perl.

To always use the highlighter for the various diff-like commands just add the following to your ~/.gitconfig file:

[pager]
    log = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less
    show = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less
    diff = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less

I added this as a new answer naught101's comment is buried and because the set up is not quite as trivial as it should be and at least on the version of Ubuntu that I have the instructions in the README don't work.

月朦胧 2024-10-29 14:15:39

我使用 --color-words 选项,它对我来说效果很好:

$ git diff --color-words | less -RS

I use --color-words option and it works fine for me :

$ git diff --color-words | less -RS
你怎么这么可爱啊 2024-10-29 14:15:39

正如@dshepherd 所说

您想要的行为现在可以在 git 本身中实现

但是 diff-highlight 位于 DOC 中,无法从 shell 中获得。
要将 diff-highlight 安装到您的 ~/bin 目录中,请按照以下步骤操作(这将节省您的输入):

$ locate diff-highlight
$ cd /usr/share/doc/git/contrib/diff-highlight  #or path you locate
$ sudo make
$ mv diff-highlight ~/bin

然后将您的 .gitconfig 配置为官方版本医生说:

[pager]
    log  = diff-highlight | less
    show = diff-highlight | less
    diff = diff-highlight | less

UPD
您也可以在不进行任何安装的情况下在最新的 git 上尝试下一步:

git diff --color-words=.

更复杂:

git diff --color-words='[^[:space:]]|([[:alnum:]]|UTF_8_GUARD)+'

as @dshepherd says:

The behaviour you want is now available in git itself

But diff-highlight is located in DOC and is not available from shell.
To install diff-highlight into your ~/bin directory follow next steps (This will save your typing):

$ locate diff-highlight
$ cd /usr/share/doc/git/contrib/diff-highlight  #or path you locate
$ sudo make
$ mv diff-highlight ~/bin

Then configure your .gitconfig as official doc says:

[pager]
    log  = diff-highlight | less
    show = diff-highlight | less
    diff = diff-highlight | less

UPD
Also you can try next on latest git without any installation:

git diff --color-words=.

More complex:

git diff --color-words='[^[:space:]]|([[:alnum:]]|UTF_8_GUARD)+'
月竹挽风 2024-10-29 14:15:39

对类似但略有不同的问题的回答中,我建议使用。 com/dandavison/delta" rel="noreferrer">Delta,这是一种现代差异后处理工具,专门支持同时突出显示单词和行的特殊需求。

Delta 具有高度可配置性(使用 diff-highlightdiff-so-fancy 的模拟模式),并包括许多其他工具中没有的功能并排视图、语法突出显示和着色合并冲突gitblame输出

Delta 文档还有一个相关项目概述,其中提到了更多广告-可以突出显示单词和线条的特殊工具。

Delta diff 格式示例

In an answer to a similar, but slightly different question, I suggest using Delta, which is a modern diff postprocessing tool that specifically supports the special desire for highlighting both words and lines at the same time.

Delta is highly configurable (with emulation modes for diff-highlight and diff-so-fancy) and includes many features not found in other tools: side-by-side views, syntax highlighting, and coloring of merge conflicts and git blame output.

The Delta documentation also has an overview of related projects that mentions a few more ad-hoc tools that can highlight both words and lines.

Delta diff formatting example

七颜 2024-10-29 14:15:39

Emacs 具有 ediff-patch-buffer 功能,应该可以满足您的需求。

在 emacs 中打开未打补丁的文件,输入 ESC-x, ediff-patch-buffer。

按照提示操作,您应该会看到文件的修补版本和原始版本的突出显示比较。

根据您的评论,以下内容将为您提供一个仅需要 dwdiff 的 bash 解决方案:

#!/bin/bash
paste -d'\n' <(dwdiff -2 -L -c <(cat $2) <(patch $2 -i $1 -o -)) <(dwdiff -1 -L -c <(cat $2) <(patch $2 -i $1 -o -))| uniq

Emacs has the ediff-patch-buffer function which should fulfill your needs.

Open the un-patched file in emacs type ESC-x, ediff-patch-buffer.

Follow the prompts and you should see a highlighted comparison of the patched and original versions of your file.

As per your comment the following will will give you a bash solution requiring only dwdiff:

#!/bin/bash
paste -d'\n' <(dwdiff -2 -L -c <(cat $2) <(patch $2 -i $1 -o -)) <(dwdiff -1 -L -c <(cat $2) <(patch $2 -i $1 -o -))| uniq
策马西风 2024-10-29 14:15:39

Diffy

GitLab 正在使用 Diffy https://github.com/samg/diffy (Ruby) 实现类似于 GitHub 和 diff-highlight 的输出:

在此处输入图像描述

Diffy 使用与 Git 相同的算法,并支持不同类型的输出,包括 GitLab 使用的 HTML 输出:

gem install diffy
echo '
  require "diffy"    
  puts Diffy::Diff.new("a b c\n", "a B c\n").to_s(:html)
' | ruby

输出:

<div class="diff">
  <ul>
    <li class="del"><del>a <strong>b</strong> c</del></li>
    <li class="ins"><ins>a <strong>B</strong> c</ins></li>
  </ul>
</div>

注意如何将 strong 添加到更改的字节中。

Diffy

GitLab is using Diffy https://github.com/samg/diffy (Ruby) to achieve output similar to GitHub and diff-highlight:

enter image description here

Diffy makes the diff itself using the same algorithm ad Git, and supports different types of outputs, including the HTML output that GitLab uses:

gem install diffy
echo '
  require "diffy"    
  puts Diffy::Diff.new("a b c\n", "a B c\n").to_s(:html)
' | ruby

Output:

<div class="diff">
  <ul>
    <li class="del"><del>a <strong>b</strong> c</del></li>
    <li class="ins"><ins>a <strong>B</strong> c</ins></li>
  </ul>
</div>

Note how strong was added to the changed bytes.

極樂鬼 2024-10-29 14:15:39

注意:这与此处找到的内容重复:如何改进 git 的差异突出显示? .不过,也在这里发布我的答案,因为它可能对一些直接找到此线程的人有帮助:)

正如之前的一些答案中所说,只有 git 的东西才有可能实现这一点。我发布此内容是因为根据您的系统,说明可能更容易遵循,但这与其他几个答案类似。

一种完全依赖 git 及其贡献的解决方案。除了 git 附带的文件之外,这不需要其他文件。所有解释均针对 Ubuntu(在 18.04LTS 上测试),在其他 Linux 系统上应该类似地工作:

  • 找到 diff-highlight contrib git 片段:
find -L /usr -name diff-highlight -type f

在我的系统上,唯一有效的答案是:

/usr/share/doc/git/contrib/diff-highlight/diff-highlight
  • 使相应的 perl 脚本可执行。就我而言,我需要执行以下操作:
sudo chmod +x /usr/share/doc/git/contrib/diff-highlight/diff-highlight
  • 更新您的 ~/.gitconfig 以获得您想要的结果,方法是添加(注意这些是 TABS,而不是 4 个空格):
[color "diff-highlight"]
    oldNormal = red
    oldHighlight = red 52
    newNormal = green
    newHighlight = green 22
  • 享受结果(注意:这只是对于差异着色+突出显示,当然,我还有其他事情在起作用,以供提示:))。

Note: this is a duplicate of what is found here: How to improve git's diff highlighting? . Posting my answer here too though, as it may be helpful to some people who find directly this thread :)

As said in some previous answers, this is possible with only git stuff. I post this as the instructions may be a bit easier to follow depending on your system, but this is similar to several other answers.

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 :) ).

diff-highligh

俯瞰星空 2024-10-29 14:15:39

当您预览差异时,我的 vim 插件 vim-gitgutter 会执行此操作。您可以在自述文件的屏幕截图中看到一个简单的示例。

计算行内差异的代码在这里< /a>.

My vim plugin vim-gitgutter does this when you preview a diff. You can see a simple example in the screenshot in the readme.

The code that calculates the intra-line differences is here.

面犯桃花 2024-10-29 14:15:39

有些工具可能已经提到过,但我将尝试在这里总结并添加一些额外的提示:

Bash/Terminal

您可以使用 colordiffdiff-so-fancy

安装

选择取决于您的系统

sudo apt-get install colordiff
# or
sudo yum install colordiff

使用

colordiff firstFile secondFile
# or for word coloring
colordiff -y firstFile secondFile

Git

如果您的 diff 尚未突出显示输出,可以使用以下命令进行配置:

git config --global color.ui auto
git config --global color.diff.wordDiff true

Vim

Vim 有一个内置突出显示功能,可以通过编辑 ~/.vimrc 并添加以下内容来启用它lines:

syntax enable
filetype plugin indent on

为了突出显示确切的字节,需要一个额外的插件,有多个插件可用于此目的,即 diffchar.vimvim-gitgutter

Some of the tools may already mentioned but I will try to summarise and add some extra tips here:

Bash/Terminal

You can use the colordiff or diff-so-fancy

Install

Pick depending on your system

sudo apt-get install colordiff
# or
sudo yum install colordiff

Usage

colordiff firstFile secondFile
# or for word coloring
colordiff -y firstFile secondFile

Git

In case your diff not already highlight the output this can configured with:

git config --global color.ui auto
git config --global color.diff.wordDiff true

Vim

Vim has a build-in highlighting that can be enabled via editing the ~/.vimrc and adding the following lines:

syntax enable
filetype plugin indent on

For highlighting the exact bytes it is required an extra plugin there are multiple plugins available for this purpose ie diffchar.vim or vim-gitgutter

╄→承喏 2024-10-29 14:15:39

是的,Vim 会执行此操作,包括突出显示行内更改的文本。
有关如何比较文件的更多详细信息,请参阅 :h diff:h 08.7

Vim 使用相当简单的算法来突出显示。
它在行中搜索第一个更改的字符,然后搜索最后一个更改的字符,并简单地突出显示它们之间的所有字符。
这意味着每行不能有多个突出显示 - Vim 中的许多设计决策都优先考虑效率。

Yes, Vim does this including the highlighting of text changed within a line.
See :h diff and :h 08.7 for more details on how to diff files.

Vim uses a fairly simple algorithm for it's highlighting.
It searches the line for the first changed character, and then the last changed character, and simply highlights all characters between them.
This means you can't have multiple highlights per line - many design decisions in Vim prioritise efficiency.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文