Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there a way to use --word-diff for LaTeX files (*.tex) and keep the standard line difference for other file types?

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?

like image 631
Evgenii Avatar asked Mar 12 '23 17:03

Evgenii


1 Answers

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.

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.

Putting these all together:

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

(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

(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.)

like image 192
torek Avatar answered Mar 15 '23 07:03

torek