如何让 diff 像 git-diff 一样工作?

发布于 2024-10-15 07:48:58 字数 506 浏览 3 评论 0原文

我喜欢 git diff 的输出格式。行间变化的颜色和 +/- 表示比 GNU diff 更容易阅读。

我可以在 git 存储库之外使用 --no-index 标志运行 git diff 并且工作正常。但是,它似乎缺少用于从递归 diff 中排除文件或子目录的 --exclude 选项。

有没有一种方法可以两全其美? (git diff 的颜色选项和 +/- 格式以及 GNU diff 的 --exclude 选项)。

我已经尝试过 colordiff,但我仍然更喜欢 git diff

I like the output formatting of git diff. The color and the +/- representation of changes between lines is easier to read than GNU diff.

I can run git diff using --no-index flag outside of a git repo and it works fine. However, it appears to be missing the --exclude option for excluding files or subdirectories from a recursive diff.

Is there a way to get the best of both worlds? (color options and +/- format of git diff and --exclude option of GNU diff).

I've experimented with colordiff, but I still prefer the output format of git diff

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

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

发布评论

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

评论(15

往事风中埋 2024-10-22 07:48:58

这将执行 +/- 而不是 <>

diff -u file1 file2

从 GNU diffutils 3.4 开始,添加了标志 --color。将两者结合起来会产生以下效果:

diff --color -u file1 file2

标志 --color 也接受一个参数,有效选项为 neveralwaysauto< /代码>。当您想要更明确地说明需要做什么时很有用。

This will do the +/- rather than < and >.

diff -u file1 file2

Since GNU diffutils 3.4 the flag --color has been added. Combining both makes the following:

diff --color -u file1 file2

The flag --color also takes an argument, valid options are never, always, or auto. Useful when you want to be more explicit on what needs to be done.

兮子 2024-10-22 07:48:58

您还可以使用 git diff --no-index -- A B (通过 手册页)。

You can also use git diff --no-index -- A B (via manpage).

迷途知返 2024-10-22 07:48:58
  1. 安装colordiff

  2. 更新您的 ~/.colordiffrc (如有必要,首先复制 /etc/colordiffrc):

    # 更像 git:
    普通=关闭
    新文本=深绿色
    旧文本=深红色
    diffstuff=深青色
    
  3. 对两个文件或 colordiff 使用 colordiff -u file1 file2 -ruN path1 path2 用于递归比较路径。

虽然不完全一样,但是非常接近。

  1. Install colordiff.

  2. Update your ~/.colordiffrc (copying /etc/colordiffrc first, if necessary):

    # be more git-like:
    plain=off
    newtext=darkgreen
    oldtext=darkred
    diffstuff=darkcyan
    
  3. Use colordiff -u file1 file2 for two files or colordiff -ruN path1 path2 for recursively comparing paths.

It's not exactly the same, but it's very close.

左秋 2024-10-22 07:48:58

这就是我的建议,它非常接近

diff -u FILE1 FILE2 | colordiff | less -R
  • colordiff:你必须安装这个
    • 在我的 Mac 上brew install colordiff
    • 在某些 Mac 上移植安装 colordiff
    • 在 Debian 或 Ubuntu 上 sudo apt-get install colordiff
    • 对于其他平台,请从主页GitHub 并关注 安装说明
  • -R:这告诉 Less 显示颜色而不是原始代码。

我最终使用了 -w 因为我不想看到空格差异。

diff -w -u FILE1 FILE2 | colordiff | less -R

编辑:正如 @Ciprian Tomoiaga 在评论中所建议的,您可以将其设为一个函数并将其也放入您的 ~/.bashrc 文件中。

function gdiff () { diff -u $@ | colordiff | less -R; }

This is what I suggest and it's pretty close

diff -u FILE1 FILE2 | colordiff | less -R
  • colordiff: You'll have to install this
  • -R: this tells Less to show colors instead of the raw codes.

I ultimately used -w because I didn't want to see whitespace diffs.

diff -w -u FILE1 FILE2 | colordiff | less -R

Edit: As suggested by @Ciprian Tomoiaga in the comment, you can make this a function and put it in your ~/.bashrc file too.

function gdiff () { diff -u $@ | colordiff | less -R; }
晨与橙与城 2024-10-22 07:48:58

根据这个答案在 Unix SE 上。与 -u 一起应该足以模仿 git diff 的输出:

diff -u --color=always file1 file2 | less -r

--color 在管道中使用时必须alwaysauto 将关闭管道中的颜色。

我只在 Windows 上使用 Git Bash 尝试过此操作,其中 less -R 只会为块的第一行着色。在这种情况下,less -r 为我修复了它。

GNU diff has a --color option since version 3.4 in late 2016 according to this answer on the Unix SE. That alongside -u should be enough to mimic the output of git diff:

diff -u --color=always file1 file2 | less -r

--color must be always when used in a pipe, auto will turn off color in pipes.

I've only tried this with Git Bash on Windows, where less -R would only color the first line of a hunk. less -r fixed it for me in that case.

涫野音 2024-10-22 07:48:58

将其放入 rc 文件中,最常见的是 .bashrc.zshrc

diff() { git diff --no-index "$1" “$2”|颜色差异; }

要求:应已安装 gitcolordiff

用法:比较 file1 file2

Place this in your rc file, most commonly it would be either .bashrc or .zshrc :

diff() { git diff --no-index "$1" "$2" | colordiff; }

requirements : git and colordiff should have been installed.

usage : diff file1 file2

诗酒趁年少 2024-10-22 07:48:58

仅使用 bashdifftputless,我们就可以非常接近 git 的输出差异。不过,由于 diff 程序员的短视,将会存在一些显着的差异。

将以下 Bash 函数定义放入由您的用户帐户自动获取的某个文件中,您将能够从命令行访问该函数:

function gdiff()
{
    local REG=`tput op`
    local GRP=`tput setaf 6`
    local ADD=`tput setaf 2`
    local REM=`tput setaf 1`

    local NL=

该函数的工作原理如下:

  1. 最终,diff 得到使用各种格式选项调用以指定如何显示文件中的更改。
  2. tput 用于将 ANSI 颜色代码插入到这些格式选项中。请注意,使用非 ANSI 终端时,您可能必须将 tput setaf 替换为 tput setf
  3. diff 的输出通过管道传输到 less 中。 -R 允许保留 ANSI 颜色。 -X 防止 less 在退出时清除屏幕。如果输出适合一个屏幕,-F 会阻止 less 作为寻呼机运行。
  4. 如果第一个参数是@full,该函数将显示除了添加和删除的行之外的所有未更改的行。

请注意此方法与 git diff 之间的以下差异:

  1. git diff 报告每个更改周围的三行上下文。不幸的是,如果您想指定上下文行数,同时指定格式选项,则 diff 似乎会抱怨并退出。 (至少在 Mac OS X Yosemite 中是这样)。感谢 diff 程序员。因此,您可以请求不包含每个更改周围的上下文行(这是默认行为),也可以通过指定 @full 作为第一个参数来请求报告文件中所有未更改的行。
  2. 由于上下文行与 git diff 不同,因此该函数报告的行号也将与 git diff 报告的行号不同。
  3. 您可能会看到报告的单行更改的存在,这是正确的行为,但当更改的文件包含插入单个空行时,会很烦人。我认为 git diff 通过其上下文可以更好地处理这个问题。如果您愿意,可以尝试将不同的选项传递给 diff 以更好地处理空格。
\n' local GRP_LABEL="${GRP}@@ %df,%dn +%dF,%dN @@${REG}" local UNCH_GRP_FMT='' [[ "${1}" == '@full' ]] && { UNCH_GRP_FMT="${GRP_LABEL}${NL}%=" shift } diff \ --new-line-format="${ADD}+%L${REG}" \ --old-line-format="${REM}-%L${REG}" \ --unchanged-line-format=" %L${REG}" \ --new-group-format="${GRP_LABEL}${NL}%>" \ --old-group-format="${GRP_LABEL}${NL}%<" \ --changed-group-format="${GRP_LABEL}${NL}%<%>" \ --unchanged-group-format="${UNCH_GRP_FMT}" \ "${@}" | less -FXR }

该函数的工作原理如下:

  1. 最终,diff 得到使用各种格式选项调用以指定如何显示文件中的更改。
  2. tput 用于将 ANSI 颜色代码插入到这些格式选项中。请注意,使用非 ANSI 终端时,您可能必须将 tput setaf 替换为 tput setf
  3. diff 的输出通过管道传输到 less 中。 -R 允许保留 ANSI 颜色。 -X 防止 less 在退出时清除屏幕。如果输出适合一个屏幕,-F 会阻止 less 作为寻呼机运行。
  4. 如果第一个参数是@full,该函数将显示除了添加和删除的行之外的所有未更改的行。

请注意此方法与 git diff 之间的以下差异:

  1. git diff 报告每个更改周围的三行上下文。不幸的是,如果您想指定上下文行数,同时指定格式选项,则 diff 似乎会抱怨并退出。 (至少在 Mac OS X Yosemite 中是这样)。感谢 diff 程序员。因此,您可以请求不包含每个更改周围的上下文行(这是默认行为),也可以通过指定 @full 作为第一个参数来请求报告文件中所有未更改的行。
  2. 由于上下文行与 git diff 不同,因此该函数报告的行号也将与 git diff 报告的行号不同。
  3. 您可能会看到报告的单行更改的存在,这是正确的行为,但当更改的文件包含插入单个空行时,会很烦人。我认为 git diff 通过其上下文可以更好地处理这个问题。如果您愿意,可以尝试将不同的选项传递给 diff 以更好地处理空格。

Using only bash, diff, tput, and less, we can closely approximate the output of git diff. There will be some notable differences, though, due to the short-sightedness of the diff programmers.

Put the following Bash function definition in some file that gets sourced automatically by your user account, and you'll be able to access the function from the command line:

function gdiff()
{
    local REG=`tput op`
    local GRP=`tput setaf 6`
    local ADD=`tput setaf 2`
    local REM=`tput setaf 1`

    local NL=

This function works as follows:

  1. Ultimately, diff gets invoked with various formatting options to specify how changes within the files will be displayed.
  2. tput is used to insert ANSI color codes into those formatting options. Note that when using non-ANSI terminals, you may have to replace tput setaf with tput setf.
  3. The output of diff is piped into less. -R allows ANSI colors to be preserved. -X prevents less from clearing the screen upon exiting. -F prevents less from operating as a pager if the output fits within one screen.
  4. If the first parameter is @full, the function will display all unchanged lines in addition to added and removed lines.

Note the following differences between this approach and git diff:

  1. git diff reports three lines of context surrounding each change. Unfortunately, diff seems to complain and exit if you want to specify the number of context lines while also simultaneously specifying formatting options. (At least it does in Mac OS X Yosemite). Thanks diff programmers. Therefore, you can either request no lines of context surrounding each change, which is the default behavior, or you can request that all unchanged lines within the file are also reported, by specifying @full as the first parameter.
  2. Because the lines of context are different from git diff, the line numbers reported by this function will also vary from those reported by git diff.
  3. You may see the presence of single-line changes reported, which is the correct behavior, but annoying when your changed file contains the insertion of single empty lines. I think git diff deals with this better, via its lines of context. You could try passing different options to diff to better deal with whitespace, if you prefer.
\n' local GRP_LABEL="${GRP}@@ %df,%dn +%dF,%dN @@${REG}" local UNCH_GRP_FMT='' [[ "${1}" == '@full' ]] && { UNCH_GRP_FMT="${GRP_LABEL}${NL}%=" shift } diff \ --new-line-format="${ADD}+%L${REG}" \ --old-line-format="${REM}-%L${REG}" \ --unchanged-line-format=" %L${REG}" \ --new-group-format="${GRP_LABEL}${NL}%>" \ --old-group-format="${GRP_LABEL}${NL}%<" \ --changed-group-format="${GRP_LABEL}${NL}%<%>" \ --unchanged-group-format="${UNCH_GRP_FMT}" \ "${@}" | less -FXR }

This function works as follows:

  1. Ultimately, diff gets invoked with various formatting options to specify how changes within the files will be displayed.
  2. tput is used to insert ANSI color codes into those formatting options. Note that when using non-ANSI terminals, you may have to replace tput setaf with tput setf.
  3. The output of diff is piped into less. -R allows ANSI colors to be preserved. -X prevents less from clearing the screen upon exiting. -F prevents less from operating as a pager if the output fits within one screen.
  4. If the first parameter is @full, the function will display all unchanged lines in addition to added and removed lines.

Note the following differences between this approach and git diff:

  1. git diff reports three lines of context surrounding each change. Unfortunately, diff seems to complain and exit if you want to specify the number of context lines while also simultaneously specifying formatting options. (At least it does in Mac OS X Yosemite). Thanks diff programmers. Therefore, you can either request no lines of context surrounding each change, which is the default behavior, or you can request that all unchanged lines within the file are also reported, by specifying @full as the first parameter.
  2. Because the lines of context are different from git diff, the line numbers reported by this function will also vary from those reported by git diff.
  3. You may see the presence of single-line changes reported, which is the correct behavior, but annoying when your changed file contains the insertion of single empty lines. I think git diff deals with this better, via its lines of context. You could try passing different options to diff to better deal with whitespace, if you prefer.
甩你一脸翔 2024-10-22 07:48:58

由于 bat 具有很好的着色效果,我测试了它是否适用于 diff令人惊讶的是,它开箱即用,效果非常好。

$ diff 文件 1 文件 2 | bat$ diff -u file1 file2 |所以

我想你可以创建一个像下面这样的函数来提高效率:

function bdiff () { diff -u $@ | bat;}

Since bat has nice colorizing, I've tested if that works with diff too and surprisingly it worked really well out of the box.

$ diff file1 file2 | bat or $ diff -u file1 file2 | bat

So I suppose you could make a function like this below to be more efficient:

function bdiff () { diff -u $@ | bat;}
涙—继续流 2024-10-22 07:48:58

您正在寻找colordiff

sudo apt-get install colordiff

You are looking for colordiff:

sudo apt-get install colordiff
饭团 2024-10-22 07:48:58

如果你没有 colordiffgit diff,你可以通过 vim 获取颜色。

cdiff() { diff -u $@ | vim -R -; }

或者简单地

cdiff() { diff -u $@ | view -; }

If you don't have colordiff or git diff, you can get color by vim.

cdiff() { diff -u $@ | vim -R -; }

or simply

cdiff() { diff -u $@ | view -; }
可是我不能没有你 2024-10-22 07:48:58

在 debian 9 中测试
diff -u --color=always file1 file2

Tested in debian 9
diff -u --color=always file1 file2

北风几吹夏 2024-10-22 07:48:58

另一种选择是从存储库外部执行此操作,以便 git 知道文件之间的差异。例如。 shell 函数类似于:

gdiff() {
    (
        dir=`pwd`
        cd ./$(git rev-parse --show-cdup)/..
        git diff  $dir/$1 $dir/$2
    )
}

The other option is to do it from outside the repository so git knows to diff between files. eg. a shell function something like:

gdiff() {
    (
        dir=`pwd`
        cd ./$(git rev-parse --show-cdup)/..
        git diff  $dir/$1 $dir/$2
    )
}
半城柳色半声笛 2024-10-22 07:48:58

使用colordiff

安装:

sudo apt-get install colordiff

用法:

colordiff -u file_one file_two

给出与git diff所示完全相同的差异。

Use colordiff:

Installation:

sudo apt-get install colordiff

Usage:

colordiff -u file_one file_two

Gives exactly same difference as shown by git diff.

暮凉 2024-10-22 07:48:58

添加

alias diff="git diff --no-index --"

到 ~/.zshrc 或 ~/.bashrc

这使用 git diff 在两个文件之间进行普通比较

add

alias diff="git diff --no-index --"

to ~/.zshrc or ~/.bashrc

This uses git diff to do ordinary diff between two files

铁憨憨 2024-10-22 07:48:58

我认为配置设置:

[color]
     ui = true

与“diff”命令的 --relative= 选项相结合可以满足您的需求。你尝试了吗?

I think the config setting :

[color]
     ui = true

combined with "diff" command's --relative=<path> option would do what you wanted. Did you try ?

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