问题描述
是否可以将--word-diff
用于LaTeX文件(* .tex),并保持其他文件类型的标准行差异?
Is there a way to use --word-diff
for LaTeX files (*.tex) and keep the standard line difference for other file types?
我要实现的是使用git diff
命令,并让git自动显示* .tex文件上的单词差异,而无需每次都编写git diff --word-diff
.同时,我希望git显示其他文件类型的标准行差异.有可能吗?
What I want to achieve is to use git diff
command and let git show the word difference on *.tex files automatically without a need to write git diff --word-diff
each time. At the same time I want git to show the standard line difference for other file types. Is that possible?
推荐答案
请参见 gitattributes
文档的生成差异文本部分.但是,要为*.tex
文件自动获取单词差异 just ,必须在此处以及其他一些文档中将其与其他信息放在一起.
See the Generating diff text section of the gitattributes
documentation. However, to automatically get word diffs just for *.tex
files, you must put this together with additional information there and in some other documents.
此外,至少在我当前的git版本(2.7.4)中,tex文件的内置正则表达式已损坏:
Also, at least in my current git version (2.7.4), the built-in regex for tex files is broken:
fatal: Invalid regular expression: \\[a-zA-Z@]+|\\.|[a-zA-Z0-9<80>-<FF>]+|[^[:sp
所以我必须更加努力地解决这个问题.
so I have to work around that even harder.
将所有这些放在一起:
$ cat .gitattributes
*.tex diff=tex
$ git config --get diff.tex.wordregex
\\[a-zA-Z]+|[{}]|\\.|[^\{}[:space:]]+
(此正则表达式直接来自gitattributes文档),再加上一个配置项和一个驱动程序:
(this regex is straight from the gitattributes documentation), plus one more configuration item and one driver:
$ 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 $2 $5
exit 0
(此脚本可能有所改进,但它显示了总体思路.由于文件不同,如果文件不同,git diff
的出口为非零值,所以需要exit 0
.幸运的是,无需进行无休止的保护.进行递归,因为git diff --word-diff path1 path2
不会重新调用gitattributes驱动程序.)
(This script might be improved, but it shows the general idea. The exit 0
is required since git diff
has a nonzero exit if the files differ, as they tend to. Fortunately there is no need to protect against endless recursion since git diff --word-diff path1 path2
does not re-invoke the gitattributes driver.)
这篇关于如何自动为* .tex文件而不是其他文件使用git diff --word-diff选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!