How to use difftool and mergetool on Windows 10 Ubuntu bash (WSL)
Asked Answered
D

4

16

I use Windows 10 Ubuntu bash, provided by Windows Subsystem for Linux. I want to use a visual diff/merge tool for git. I installed p4merge on Windows (followed this artice) and configured the git .gitconfig file with the following way (I adjusted the paths to be accessible from Windows 10 Ubuntu bash):

[merge]
    tool = p4merge
[mergetool "p4merge"]
    path = /mnt/c/Program Files/Perforce/p4merge.exe
[mergetool]
    prompt = false
[diff]
    tool = p4merge
[difftool "p4merge"]
    path = /mnt/c/Program Files/Perforce/p4merge.exe
[difftool]
    prompt = false

Additionally, I added the following folder to the bash PATH variable in .bashrc to make it callable from anywhere:

 export PATH=$PATH:"/mnt/c/Program Files/Perforce"

So my problem is that if I call git difftool in the bash - to investigate the changes with p4merge - I got the following messages

  • in bash: Unable to translate current working directory. Using C:\Windows\system32.
  • the p4merge application is started right after the call but gives the following message: Error: ... point to an invalid file.

If I understand right, this problem may emanate from the fact that a Windows program (namely p4merge) could not find a file that is referenced with a Linux file path (e.g. /mnt/c/..).

Is there any way to solve this kind of problem? (Maybe it is a more general problem: using Linux path from Windows application.)

Anyway, I do not insist on using p4merge but any similar visual tool to compare differences and to make merge possible.

Any information you can provide me would be greatly appreciated.

Doscher answered 14/8, 2017 at 19:20 Comment(0)
R
4

Create file p4mergebash.sh and set $PATH:

mkdir -p ~/bin
touch ~/bin/p4mergebash.sh
chmod +x ~/bin/p4mergebash.sh
echo 'export PATH=$PATH:/mnt/c/Program\ Files/Perforce' >> ~/.bashrc
source ~/.bashrc

p4mergebash.sh content:

#!/bin/sh

LOCAL="$1"
REMOTE="$2"

case "$LOCAL"
in
    *)
    L=$(echo "C:/Users/<username>/AppData/Local/lxss/rootfs${LOCAL}" | sed 's,/,\\\\,g')
    ;;
esac

case "$REMOTE"
in
    *)
    R=$(echo `git rev-parse --show-toplevel`/"$REMOTE" | sed 's,/mnt/c/,C:/,g' | sed 's,/,\\\\,g')
    ;;
esac

echo "$L"
echo "$R"
p4merge.exe "$L" "$R"

Above script assumes your AppData and your git repo are on C: drive. Insert your username into the angle brackets (i.e. <username>).

Note: Ensure there are no CRLF's in p4mergebash.sh.

Then set git config:

git config --global diff.tool p4mergebash
git config --global difftool.p4mergebash.cmd '~/bin/p4mergebash.sh $LOCAL $REMOTE'
git config --global difftool.prompt false
Resign answered 16/10, 2017 at 17:16 Comment(2)
Worked very well for me. (Using meld) In my case however, I had to slightly change the contents of p4mergebash.sh to make it match the LOCAL so that it would fetch from my tmp directory for remote as well. R=$(echo "C:/Users/<username>/AppData/Local/lxss/rootfs${REMOTE}" | sed 's,/,\\\\,g')Bridesmaid
@Resign L=$(echo "C:/Users/ysuh/AppData/Local/Packages/CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc/LocalState/rootfs${LOCAL}" | sed 's,/,\\\\,g') I had to update the bash script's value for L. /mnt/c/Program\ Files/Perforce/p4merge.exe "$L" "$R" I also had to hardcode the executable path. I couldn't get it to work with just updating the PATH via .bash_profile.Wessling
Z
23

Solution to this problem is simplified!

Thanks to the Windows 10 version 1903 update, accessing the Linux subsystem from Windows 10 is now possible. It is much more easier.

Just make sure that git config --global --list has these lines. That's all!

diff.tool=p4merge
merge.tool=p4merge
difftool.p4merge.cmd=/mnt/c/Program\ Files/Perforce/p4merge.exe "$(wslpath -aw $LOCAL)" "$(wslpath -aw $REMOTE)"
mergetool.p4merge.cmd=/mnt/c/Program\ Files/Perforce/p4merge.exe "$(wslpath -aw $BASE)" "$(wslpath -aw $LOCAL)" "$(wslpath -aw $REMOTE)" "$(wslpath -aw $MERGED)"
mergetool.p4merge.trustexitcode=false

Takeaways:

  • Linux subsystem now resides at \\wsl$\{distro name}\ path.
  • Which means you can access the tmp folder at \\wsl$\Ubuntu\tmp\. Even from the File Explorer!
  • wslpath -aw command does the heavy lifting for you.
  • The command now properly translates Linux relative /tmp/whatever path to a Windows 10 relative path.
Zavala answered 11/6, 2019 at 4:13 Comment(4)
Thanks a lot for this answer. The only addition is that you most probably want to use wslpath -am to convert \ to /Pulley
In my experience, it didn't need -am. But defiantly, you can add it just in case. Thanks for pointing out!Zavala
I've added the flag to force the unix line ending on p4merge p4merge.exe -le unix. If not it kept changing the line endings.Misalliance
Note that if you happen to use WinMerge, the last parameter (with $MERGED) needs the /o flag for the saving to work correctly.Unpeg
S
9

For Beyond Compare 4, add these lines to your .gitconfig file:

[merge]
    tool = BeyondCompare4
    guitool = BeyondCompare4
[diff]
    guitool = beyondcompare4
[difftool "beyondcompare4"]
    cmd = /mnt/c/Program\\ Files/Beyond\\ Compare\\ 4/bcomp.exe "$(wslpath -aw $LOCAL)" "$(wslpath -aw $REMOTE)"
[mergetool "BeyondCompare4"]
    cmd = /mnt/c/Program\\ Files/Beyond\\ Compare\\ 4/bcomp.exe "$(wslpath -aw $LOCAL)" "$(wslpath -aw $REMOTE)" "$(wslpath -aw $BASE)" "$(wslpath -aw $MERGED)"
Shore answered 2/12, 2020 at 22:54 Comment(2)
I had to add the tool = beyondcompare4 line under [diff], otherwise when I entered git difftool HEAD^! it opened the merge window. And guitool isn't really needed, tool is enough. Other than that, it works. Thanks a lot!Inextirpable
@FabiosaysReinstateMonica no problem. Thank you too.Shore
R
4

Create file p4mergebash.sh and set $PATH:

mkdir -p ~/bin
touch ~/bin/p4mergebash.sh
chmod +x ~/bin/p4mergebash.sh
echo 'export PATH=$PATH:/mnt/c/Program\ Files/Perforce' >> ~/.bashrc
source ~/.bashrc

p4mergebash.sh content:

#!/bin/sh

LOCAL="$1"
REMOTE="$2"

case "$LOCAL"
in
    *)
    L=$(echo "C:/Users/<username>/AppData/Local/lxss/rootfs${LOCAL}" | sed 's,/,\\\\,g')
    ;;
esac

case "$REMOTE"
in
    *)
    R=$(echo `git rev-parse --show-toplevel`/"$REMOTE" | sed 's,/mnt/c/,C:/,g' | sed 's,/,\\\\,g')
    ;;
esac

echo "$L"
echo "$R"
p4merge.exe "$L" "$R"

Above script assumes your AppData and your git repo are on C: drive. Insert your username into the angle brackets (i.e. <username>).

Note: Ensure there are no CRLF's in p4mergebash.sh.

Then set git config:

git config --global diff.tool p4mergebash
git config --global difftool.p4mergebash.cmd '~/bin/p4mergebash.sh $LOCAL $REMOTE'
git config --global difftool.prompt false
Resign answered 16/10, 2017 at 17:16 Comment(2)
Worked very well for me. (Using meld) In my case however, I had to slightly change the contents of p4mergebash.sh to make it match the LOCAL so that it would fetch from my tmp directory for remote as well. R=$(echo "C:/Users/<username>/AppData/Local/lxss/rootfs${REMOTE}" | sed 's,/,\\\\,g')Bridesmaid
@Resign L=$(echo "C:/Users/ysuh/AppData/Local/Packages/CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc/LocalState/rootfs${LOCAL}" | sed 's,/,\\\\,g') I had to update the bash script's value for L. /mnt/c/Program\ Files/Perforce/p4merge.exe "$L" "$R" I also had to hardcode the executable path. I couldn't get it to work with just updating the PATH via .bash_profile.Wessling
M
0

Here are the .gitconfig lines I use for p4merge for merge and diff tool. Also to use VS code as commit message editor:

[diff]
    tool = p4merge
[merge]
    tool = p4merge
[difftool "p4merge"]
    cmd = /mnt/c/Program\\ Files/Perforce/p4merge.exe \"$(wslpath -aw $LOCAL)\" \"$(wslpath -aw $REMOTE)\"
[mergetool "p4merge"]
    cmd = /mnt/c/Program\\ Files/Perforce/p4merge.exe -le unix \"$(wslpath -aw $BASE)\" \"$(wslpath -aw $LOCAL)\" \"$(wslpath -aw $REMOTE)\" \"$(wslpath -aw $MERGED)\"
    trustexitcode = false
    keepBackup = false
[core]
    editor = code -w

I like BeyondCompare better for 3-way merge, but p4merge is the best free option in my opinion. To get VS Code working with wsl, see this tutorial.

Misalliance answered 1/2, 2022 at 22:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.