如何为 *.tex 文件自动使用 git diff --word-diff 选项而不是其他文件?

How to automatically use git diff --word-diff option for *.tex files but not others?

有没有办法对 LaTeX 文件 (*.tex) 使用 --word-diff 而对其他文件类型保持标准行差异?

我想要实现的是使用git diff命令,让git自动显示*.tex文件上的单词差异,而不需要每次都写git diff --word-diff。同时我希望 git 显示其他文件类型的标准行差异。这可能吗?

参见the Generating diff text section of the gitattributes documentation。但是,要自动获取 *.tex 文件的单词差异 just,您必须将其与附加信息放在一起并放在其他一些文档中。

此外,至少在我当前的 git 版本 (2.7.4) 中,tex 文件的内置正则表达式已损坏:

fatal: Invalid regular expression: \[a-zA-Z@]+|\.|[a-zA-Z0-9<80>-<FF>]+|[^[:sp

所以我必须更加努力地解决这个问题。

将这些放在一起:

$ cat .gitattributes
*.tex   diff=tex
$ git config --get diff.tex.wordregex
\[a-zA-Z]+|[{}]|\.|[^\{}[:space:]]+

(此正则表达式直接来自 gitattributes 文档),再加上一个配置项和一个驱动程序:

$ git config --get diff.tex.command
git-word-diff-driver
$ cat ~/scripts/git-word-diff-driver
#! /bin/sh
#
# args are:
# path old-file old-hex old-mode new-file new-hex new-mode
git diff --word-diff  
exit 0

(这个脚本可能会有所改进,但它显示了总体思路。exit 0 是必需的,因为如果文件不同,git diff 有一个非零退出,因为它们倾向于。幸运的是无需防止无限递归,因为 git diff --word-diff <em>path1</em> <em>path2</em> 不会重新调用 git 属性驱动程序。)