Like @LucasTrzesniewski said, you can use --ext-diff
from the command line to set a diff for the current session.
You can also use the .gitattributes to set the git-diff perfile.
A git diff implementation exists of 2 parts:
- A definition in
$GIT_DIR/config
or $HOME/.gitconfig
- A bound between a file and a definition in
gitattributes
Git has choosing for this design to separate the executable code from the source code, this makes it impossible for a git clone
or other git command to run any harmful code.
Writing a definition
To write a definition, we start with a header consisting of [diff "namehere"]
, then followed by a line-break.
The next line consists of the command definition, this line is as follows: command = commandlinehere
. This command is then called with 7 arguments if it is ran, these are documented for the GIT_EXTERNAL_DIFF enviroment vriable in the docs.
GIT_EXTERNAL_DIFF
When the environment variable GIT_EXTERNAL_DIFF is
set, the program named by it is called, instead of the diff invocation
described above. For a path that is added, removed, or modified,
GIT_EXTERNAL_DIFF
is called with 7 parameters:
path old-file old-hex old-mode new-file new-hex new-mode where:
<old|new>-file are files GIT_EXTERNAL_DIFF can use to read the
contents of <old|new>,
<old|new>-hex are the 40-hexdigit SHA-1 hashes,
<old|new>-mode are the octal representation of the file modes.
The file parameters can point at the user’s working file (e.g.
new-file in "git-diff-files"), /dev/null (e.g. old-file when a new
file is added), or a temporary file (e.g. old-file in the index).
GIT_EXTERNAL_DIFF
should not worry about unlinking the temporary file, it is removed when GIT_EXTERNAL_DIFF
exits.
For a path that is unmerged, GIT_EXTERNAL_DIFF
is called with 1
parameter, <path>.
For each path GIT_EXTERNAL_DIFF
is called, two environment variables,
GIT_DIFF_PATH_COUNTER
and GIT_DIFF_PATH_TOTAL
are set.
The total example looks like this:
[diff "jcdiff"]
command = j-c-diff
Writing the gitattributes
We need to modify our gitattributes to use our custom driver. The gitattributes file consist of a syntax similar to filename value [value2 [value3 [value4 [...]]]]
.
Examples:
* diff=jcdiff
We to use our custom git diff for every file.
*.java diff=javadiff
*.python diff=pythondiff
Use javadiff for java files, pythondiff for python files.
* diff=globaldiff
*.java diff=javadiff
Use javadiff for java files, globaldiff for remaining files.
Configuring git to automatically set --ext-diff
You can add an alias using git config alias.showobject 'show --ext-diff'
to define a new command called git showobject
that automatically uses our filter.