28

I am trying to use p4merge with git but I am getting:

Error starting p4merge: "path/myFile" is (or points to) an invalid file (this lists the BASE, LOCAL, REMOTE, and standard version of the file).

Git tells me about the conflict then it asks if I wanna start the mergetool configured (p4merge) and then I get the error above.

Additional note: it happens with any file!

Any clue about what this is and how to fix it?

Brett Veenstra
  • 47,674
  • 18
  • 70
  • 86
JohnIdol
  • 48,899
  • 61
  • 158
  • 242
  • Maybe include your section in .gitconfig where you configure the merge tool and the full error message? Do you have spaces in a filename or something? – araqnid May 14 '09 at 22:51
  • nope no spaces in filename - i configured git using this question: http://stackoverflow.com/questions/426026/git-on-windows-how-do-you-set-up-a-mergetool – JohnIdol May 14 '09 at 23:15
  • Possible duplicate of [git diff tmp file invalid on windows when using external program on Windows 7](http://stackoverflow.com/questions/16551253/git-diff-tmp-file-invalid-on-windows-when-using-external-program-on-windows-7) – Powerslave Apr 29 '16 at 11:50

2 Answers2

40

This worked for me using msysGit on windows 7:

git config --global merge.tool p4merge
git config --global mergetool.p4merge.cmd 'p4merge $BASE $LOCAL $REMOTE $MERGED'

Not sure why but the quoting screwed things up for me.

Mike Glenn
  • 3,359
  • 1
  • 26
  • 33
  • Switched from one Win 8 machine to another, both using git bash and had to do this also, thanks! – Sami Rajala Sep 27 '13 at 06:10
  • Guess I should have mentioned that I use Powershell, so I'm not sure on about cmd.exe. – Mike Glenn Jan 21 '14 at 22:13
  • 3
    I used `git config --global mergetool.p4merge.cmd '"C:\Install Dir Has Spaces\p4merge.exe" $BASE $LOCAL $REMOTE $MERGED'` – Michael Mar 06 '14 at 14:59
  • On windows command prompt, second command failed saying wrong usage. So I executed that on git bash and it worked. – IsmailS May 30 '14 at 10:02
  • I did this in Mac terminal and now I get this error: 'p4merge: command not found'. Any suggestions? – Engin Yapici Jul 24 '14 at 21:24
  • @Engin make sure p4merge is available in your path. – Mike Glenn Sep 24 '14 at 00:11
  • @MikeGlenn, Thank you! You are the first person to share a valid config for my MINGW64 install on Windows 7. Been pulling my hair out for the past 30 minutes. – Shadoninja Apr 10 '16 at 18:47
  • @Michael: I had to invert the quotes like so to make it work: `git config --global mergetool.p4mergetool.cmd "'C:/Program Files/Perforce/p4merge.exe' $BASE $LOCAL $REMOTE $MERGED"` – pintxo Jun 22 '16 at 08:47
9

You will see here my config for DiffMerge or KDiff3.

Based on that, I would recommend for p4merge:

git config --global merge.tool merge
git config --global mergetool.merge.cmd "merge.sh \"$PWD/$LOCAL\" \"$PWD/$BASE\" \"$PWD/$REMOTE\" \"$PWD/$MERGED\""

and merge.sh being a wrapper (copied in a directory referenced by your PATH environment variable), able to take into account the case where no BASE exists.
(when a file is created in two different branches being then merged, there would be no common ancestor for that file)

#!/bin/sh

# Passing the following parameters to mergetool:
#  local base remote merge_result

alocal=$1
base=$2
remote=$3
result=$4

if [ -f $base ]
then
    p4merge.exe -dl "$base" "$alocal" "$remote" "$result" 
else
    p4merge.exe -dl "$result" "$alocal" "$remote" "$result" 
fi

You may note:

  • the use of PWD in the config of the merge
  • the use of "merge" as name of the merge.tool name (since the actual tool is called in the merge.sh script, where you can switch between any number of merge tool you want)
  • the use of double quotes around $base, $alocal, $remote, $result within the script
  • the conditional path for calling the tool, based on the existence of a "base" file.
  • the need to always have 3 files to merge as parameters (even when 'base' does not exist...)

Just tested it (it turns out, you can download and install only p4merge -- section Client/Visual Merge Tool --, even if you do not have any other P4 product installed).

With the settings describe above, MSysGit1.6.3, DOS session or Git bash session:
It just worksTM.


Update msysgit 1.7.x

Benjol mentions in the comments:

p4merge is now supported natively by msysgit.

This means you can just do:

git config --global merge.tool p4merge
# and I recommend 
git config --global mergetool.keepBackup false
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • You've got "merge.sh twice in the second line of your first code block, but with that corrected, everything works. Thanks! – Benjol Dec 03 '09 at 10:30
  • I just tried this again on a new machine. Seems to me that if you set the config from bash, the `$PWD` and `$LOCAL` variables get expanded in the config file. I had to go and edit the `%USERPROFILE$\.gitconfig` file manually. Maybe an error on my part, but just in case someone else has the same problem. – Benjol Dec 03 '10 at 23:39
  • @Benjol: interesting, I will test it again also. Was your new machine different from your first tries? (newer Os?) – VonC Dec 03 '10 at 23:56
  • @VonC, Vista, yes. Another tweak which may be worth mentioning is `keepBackup = false` (.orig files drive me crazy) – Benjol Dec 04 '10 at 00:14
  • 2
    Note: according to the 3rd comment [here](http://stackoverflow.com/questions/426026/git-on-windows-how-do-you-set-up-a-mergetool/436040#436040), p4merge is now supported natively by msysgit. I'm not sure since which version. This means you can just do `git config --global merge.tool p4merge` (and I recommend `git config --global mergetool.keepBackup false`) – Benjol Mar 22 '11 at 13:31
  • @Benjol: Thank you for the information, I have included it in the answer. – VonC Mar 22 '11 at 14:19