Using Custom Diff Tool with `Git Show`

With git I can set a custom diff tool that is used for certain file extensions by the following in .git/config

[diff "csv_diff"]
    command = Tools/csv_diff

and this in .gitattributes (in the root of the repository)

*.csv diff=csv_diff

This works when using git diff, but it doesn't work with git show. My question is, how do I use a custom tool with git show?

10

4 Answers

Looks like you're looking for the --ext-diff option.

Here's what git show docs say about it:

--ext-diff

Allow an external diff helper to be executed. If you set an external diff driver with gitattributes, you need to use this option with git-log and friends.

3

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
Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.