git 如何在统一的差异文件中可视化每个字符的差异?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3231759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:44:16  来源:igfitidea点击:

How can I visualize per-character differences in a unified diff file?

gitvimdiffpatch

提问by Adam Monsen

Say I get a patch created with git format-patch. The file is basically a unified diff with some metadata. If I open the file in Vim, I can see which lines have been modified, but I cannot see which charactersin the changed lines differ. Does anyone know a way (in Vim, or some other free software that runs on Ubuntu) to visualize per-character differences?

假设我得到了一个用git format-patch. 该文件基本上是带有一些元数据的统一差异。如果我在 Vim 中打开文件,我可以看到哪些行被修改,但我看不到更改行中哪些字符不同。有谁知道一种方法(在 Vim 中,或在 Ubuntu 上运行的其他一些免费软件中)来可视化每个字符的差异?

A counter example where per-character diff is visualized is when executing vimdiff a b.

每个字符差异可视化的反例是在执行vimdiff a b.

update Fri Nov 12 22:36:23 UTC 2010

2010 年 11 月 12 日星期五 22:36:23 UTC 更新

diffpatch is helpfulfor the scenario where you're working with a single file.

diffpatch对于处理单个文件的场景很有帮助

update Thu Jun 16 17:56:10 UTC 2016

2016 年 6 月 16 日星期四 17:56:10 UTC 更新

Check out diff-highlight in git 2.9. This script does exactlywhat I was originally seeking.

查看git 2.9 中的 diff-highlight。这个脚本正是我最初寻求的。

采纳答案by legoscia

Given your references to Vim in the question, I'm not sure if this is the answer you want :) but Emacs can do this. Open the file containing the diff, make sure that you're in diff-mode(if the file is named foo.diffor foo.patchthis happens automatically; otherwise type M-xdiff-modeRET), go to the hunk you are interested in and hit C-c C-bfor refine-hunk. Or step through the file one hunk at a time with M-n; that will do the refining automatically.

鉴于您在问题中对 Vim 的引用,我不确定这是否是您想要的答案:) 但 Emacs 可以做到这一点。打开包含DIFF文件,确保您在是diff-mode(如果该文件名为foo.difffoo.patch自动发生这种情况,否则型M-xdiff-modeRET),请你有兴趣,并击中了大块C-c C-brefine-hunk。或者使用M-n;一次一大块地浏览文件 这将自动进行精炼。

回答by yingted

In git, you can merge without committing. Merge your patch first, then do:

在 git 中,你可以不提交就合并。首先合并您的补丁,然后执行:

git diff --word-diff-regex=.

Note the dot after the equals sign.

注意等号后面的点。

回答by ntc2

Here are some versions with less noisy output than git diff --word-diff-regex=<re>and that require less typing than, but are equivalent to, git diff --color-words --word-diff-regex=<re>.

以下是一些输出噪音小于git diff --word-diff-regex=<re>并且需要较少输入但等效于 的版本git diff --color-words --word-diff-regex=<re>

Simple (does highlight space changes):

简单(确实突出显示空间变化):

git diff --color-words

Simple (highlights individual character changes; does not highlight space changes):

简单(突出显示单个字符更改;不突出显示空格更改):

git diff --color-words=.

More complex (does highlight space changes):

更复杂(确实突出空间变化):

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

In general:

一般来说:

git diff --color-words=<re>

where <re>is a regexp defining "words" for the purpose of identifying changes.

where<re>是一个正则表达式,用于定义“单词”以识别更改。

These are less noisy in that they color the changed "words", whereas using just --word-diff-regex=<re>surrounds matched "words" with colored -/+markers.

这些噪声较小,因为它们为更改的“单词”--word-diff-regex=<re>着色,而仅使用带有彩色-/+标记的匹配“单词”环绕。

回答by Justin M. Keyes

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

The above regex (from Thomas Rast) does a decent job of separating diff fragments at the punctuation/character level (while not being as noisy as --word-diff-regex=.).

上面的正则表达式(来自 Thomas Rast)在标点符号/字符级别分离差异片段方面做得不错(虽然不像 那样嘈杂--word-diff-regex=.)。

I posted a screenshot of the resulting output here.

我在此处发布了结果输出的屏幕截图。



Update:

更新:

This articlehas some great suggestions. Specifically, the contrib/tree of the git repo has a diff-highlightperl script that shows fine-grained highlights.

这篇文章有一些很好的建议。具体来说,contrib/git repo的树有一个diff-highlightperl 脚本,可以显示细粒度的亮点。

Quick start to use it:

快速开始使用:

$ curl https://git.kernel.org/cgit/git/git.git/plain/contrib/diff-highlight/diff-highlight > diff-highlight
$ chmod u+x diff-highlight
$ git diff --color=always HEAD~10 | diff-highlight | less -R

回答by walnutz

If you have nothing against installing NodeJS, there's a package called "diff-so-fancy" (https://github.com/so-fancy/diff-so-fancy), which is very easy to install and works perfectly:

如果你不反对安装 NodeJS,有一个名为“diff-so-fancy”的包(https://github.com/so-fancy/diff-so-fancy),它非常容易安装并且运行完美:

npm install -g diff-so-fancy
git diff --color | diff-so-fancy | less -R

Edit: Just found out it's actually a wrapper for the official diff-highlight... At least it's easier to install for perlophobes like me and the GitHub page is nicely documented :)

编辑:刚刚发现它实际上是官方 diff-highlight 的包装器......至少它更容易安装像我这样的 perlophobes 并且 GitHub 页面有很好的记录:)

回答by thegeek

Am not aware of per character difference tool, but there is a per word difference tool: wdiff.

我不知道每个字符差异工具,但有一个每个单词差异工具:wdiff。

refer examples Top 4 File Difference Tools on UNIX / Linux – Diff, Colordiff, Wdiff, Vimdiff.

参考示例UNIX/Linux 上的 4 大文件差异工具 - Diff、Colordiff、Wdiff、Vimdiff

回答by Adam Monsen

After a little research, I notice this question has come up twice recently on the main Vim mailing list. The NrrwRgn pluginwas mentioned bothtimes(make two narrow regions and diff them). Using NrrwRgn as described by Christian Brabandt feels more like a workaround than a solution, but maybe that's good enough.

经过一番研究,我注意到这个问题最近在 Vim 的主要邮件列表中出现了两次。该NrrwRgn插件提到两个(使两个窄的区域差异比较及它们)。使用 Christian Brabandt 描述的 NrrwRgn 感觉更像是一种变通方法而不是解决方案,但也许这已经足够了。

I tried out NrrwRgn and it, together with :diffthis, was indeed useful for illustrating per-character differences within parts of a single file. But it took many keystrokes. My Vimscript is pretty rusty, but it could likely be scripted. Maybe NrrwRgn could be enhanced to provide the desired functionality.

我尝试了 NrrwRgn,它与 :diffthis 一起用于说明单个文件部分内每个字符的差异确实很有用。但是需要敲很多键。我的 Vimscript 非常生疏,但它可能是脚本化的。也许可以增强 NrrwRgn 以提供所需的功能。

Thoughts?

想法?