彩色 Git 与 HTML 的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2013091/
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-08-29 01:47:25  来源:igfitidea点击:

Coloured Git diff to HTML

htmlgitcolorsdiffterminal

提问by hendry

I enjoy using git diff --color-wordsto clearly see the words that have changed in a file:

我喜欢git diff --color-words用来清楚地看到文件中已更改的单词:

Screenshot

截屏

However I want to share that diff with someone without git or a colour terminal for that matter. So does anyone know of a tool or trick that can convert colour escaped terminal outputinto HTML?

但是,我想与没有 git 或彩色终端的人分享这个差异。那么有人知道可以将颜色转义的终端输出转换为 HTML的工具或技巧吗?

回答by hendry

wget "http://www.pixelbeat.org/scripts/ansi2html.sh" -O /tmp/ansi2html.sh
chmod +x /tmp/ansi2html.sh
git diff --color-words --no-index orig.txt edited.txt | \
/tmp/ansi2html.sh > 2beshared.html

What I really needed was an ANSIto HTML converter. And I found a very decent one on http://www.pixelbeat.org/.

我真正需要的是一个ANSI到 HTML 的转换器。我在http://www.pixelbeat.org/上找到了一个非常不错的。

NOTE: You might not see any coloration unless you include --coloror --color-words, probably because piping causes git diff to exclude colors.

注意:除非您包含--color--color-words,否则您可能看不到任何着色,可能是因为管道导致 git diff 排除颜色。

NOTE 2: You may need to install gnu sed and awk, especially if you're on a Mac. Do so with brew install gnu-sed gawk. You may need to add them to your path manually, too, e.g. with ln -s /usr/local/Cellar/gnu-sed/4.2.2/bin/gsed /usr/local/bin/.

注意 2:您可能需要安装 gnu sed 和 awk,特别是如果您使用的是 Mac。这样做brew install gnu-sed gawk。您可能还需要手动将它们添加到您的路径中,例如使用ln -s /usr/local/Cellar/gnu-sed/4.2.2/bin/gsed /usr/local/bin/.

回答by Michel Gokan

download diff2html, extract it and convert diff to html with this command:

下载 diff2html,提取它并使用以下命令将 diff 转换为 html:

$ diff2html file1.txt file2.txt > diff-demo1.htm

There is more ... take a look at thisquestion.

还有更多......看看这个问题。

Orafter gitting:

或者在gitit之后:

git diff --color-words --no-index orig.txt /tmp/edited.txt > myfile

download both ansifilter from thislocation. and use this command to convert myfile to html format

这个位置下载 ansifilter 。并使用此命令将 myfile 转换为 html 格式

ansifilter -i myfile -H -o myfile2.html

so ... this is exactly what you want ;)

所以......这正是你想要的;)

回答by rtfpessoa

If you want to see clean diffswith line similarity matching, better word comparison, syntax highlightand more check out diff2htmlwhich is very customizablegit diff to HTML presenter.

如果您想查看具有行相似性匹配更好的单词比较语法突出显示等的干净差异,请查看diff2html,这是非常可定制的git diff 到 HTML 演示者。

To use it on the command line you can check diff2html-cliwhich is a simple CLI written in Node.js.

要在命令行上使用它,您可以查看diff2html-cli,这是一个用 Node.js 编写的简单 CLI。

So you can share your diffs with colleagues it integrates with diffy.org.

因此,您可以与与diffy.org集成的同事分享您的差异。

Check out a diff sample here.

在此处查看差异示例。

回答by slebetman

Don't know any tool to do exactly what you want. But here's a piece of code I often use to output html formatted colored diff: simplediff

不知道有什么工具可以做你想做的事。但是这里有一段我​​经常用来输出 html 格式的彩色差异的代码:simplediff

It's available in PHP and Python. The output tags the differences using <del>and <ins>tags so you can easily color them using CSS.

它在 PHP 和 Python 中可用。输出使用<del><ins>标签标记差异,因此您可以使用 CSS 轻松地为它们着色。

回答by rouble

You may want to checkout the github project rmed, which provides a command line tool, that generates static shareable html diffs using vimdiff.

您可能想查看 github 项目rmed,它提供了一个命令行工具,它使用 vimdiff 生成静态可共享的 html 差异。

回答by Samir Das

I have created a perl script to generate html table for the git diff. To use this script first you have to collect the diff by using git diff sha1s... > diff.file, then run diff2html.pl diff.file. Visit:

我创建了一个 perl 脚本来为 git diff 生成 html 表。要首先使用此脚本,您必须使用 git diff sha1s... > diff.file 收集差异,然后运行 ​​diff2html.pl diff.file。访问:

http://kernel-demystified.com/forum/index.php/topic,23.msg28.html#new

http://kernel-demystified.com/forum/index.php/topic,23.msg28.html#new

回答by Pablo Daniel Estigarribia Davy

when I use git bash, if you use git showyou only need to copy and paste what is shown in the console and it copies the colours correctly.

当我使用 git bash 时,如果你使用git show你只需要复制和粘贴控制台中显示的内容,它就会正确复制颜色。