10

我希望git diff输出所有文件的常规差异,* .tex 除外。对于 *.tex 文件,我想查看 .tex 文件的输出git diff --word-diff

我在玩.gitattributesand .gitconfig,但我得到的最远的结果是获得一个 .tex 文件的部分显示,然后是崩溃。

有可能得到这种行为吗?

我的.gitattributes

*.tex diff=latex

.gitconfig

[diff "latex"]
    wordRegex = "\\\\[a-zA-Z]+|[{}]|\\\\.|[^\\{}[:space:]]+"
    command = ~/bin/word-diff.sh

word-diff.sh

#!/bin/sh 
git --no-pager diff --color-words "$2" "$5"
4

1 回答 1

6

Can you post (or link to) a complete example? I've set up exactly what you've described and it seems to work fine. That is, the diff output highlights words rather than lines, and all of the changes I made were included (that is, it didn't crap out early or anything). The only anomaly was that diff would always exit with:

external diff died, stopping at foo.tex.

This happens because git diff, when run in the manner in which you're using it, behaves like the standalone diff tool: it exits with a nonzero exit code if the files differ. Git doesn't expect this behavior; it assumes that your custom command will always execute successfully and that error exit codes indicate an actual problem.

Git already knows that the files are difference and does not require the external command to make this determination. The external command is strictly a formatting tool.

If you modify your diff-words.sh script to always exit successfully, things should work out better:

#!/bin/sh
git --no-pager diff --color-words "$2" "$5"
exit 0
于 2011-09-25T00:53:38.530 回答