Git and KDiff3 on the Ubuntu subsystem

I use KDiff3 (executable kdiff3.exe) in Git Bash as git difftool and I encountered a problem to use it under the Ubuntu subsystem.

The Ubuntu subsystem has already Git installed. So I copied my Git global configuration from Git Bash (changing the path accordingly):

[user]
    email = ...
    name = ...

[merge]
    tool = kdiff3

[mergetool "kdiff3"]
    path = "/mnt/c/Program Files/KDiff3/kdiff3.exe"

[diff]
    tool = kdiff3
    guitool = kdiff3

[difftool "kdiff3"]
    path = "/mnt/c/Program Files/KDiff3/kdiff3.exe"

[core]
    autocrlf = true

Then I encountered an error:

Opening of these files failed

- [filename].[extension] (A)

The program window opened correctly, but only the right side file (B) was displayed.

The error is repeatable every time. What can cause this error?


Solution 1:

The problem is that Git uses temporary files during these operations and paths to temporary files might be absolute paths. You can see what paths are by changing the diff command to echo:

[difftool "kdiff3"]
    path = "/mnt/c/Program Files/KDiff3/kdiff3.exe"
    cmd = "echo $LOCAL $REMOTE"

The output will be something like:

/tmp/BPI1A2_Layout.js Layout.js

KDiff3 from Windows can access Layout.js because it is relative path. The path /tmp/BPI1A2_Layout.js doesn't exist on Windows, so KDiff3 can not find the file.

You could use the non-default TMPDIR for Git commands:

TMPDIR=".tmp" git difftool

Now the paths would be

.tmp/BPI1A2_Layout.js Layout.js

Both are now relative under the same directory structure which Windows is able to access and KDiff3 works as expected.

If it is OK to use a .tmp directory inside the project folder then a easy workaround is to create an alias:

alias git="TMPDIR=.tmp git"