10

When running git mergetool, I'd like to have git select the merge tool used based on the file extension. How can I do this?

A similar question was asked here: Git: configure patterns for difftool and mergetool and the answer was to write a custom merge driver. However, it seems like this would be executed upon git merge, whereas I would like the merge tool to be chosen upon git mergetool.

It seems like there must be some way to specify this with .gitattributes, but I can't seem to figure out how. Any advice?

elsevers
  • 532
  • 1
  • 4
  • 16

1 Answers1

7

One solution (since my old answer isn't a good fit for mergetool) is to set as a mergetool a wrapper script.

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

That wrapper script customMergeTool.sh would:

  • check what $BASE is (in particular, its file extension)
  • call the appropriate merge tool based on that extension
  • return the exit status of that mergetool

Here is for instance the script that the OP elsevers came up with: merge-wrapper.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Good idea! Suppose that I have a set of file extensions for which the script will call specific merge tools that are specified via `mergetool.xxxx.cmd`, and then I have a default merge tool which is a git-supported tool (i.e. p4merge, kdiff3, etc). In the case that my extension matches one of the specific merge tools, my script could run `git config mergetool.xxxx.cmd` -- but in the case of a git-supported merge tool, where git invokes the merge tool itself and `cmd` is not specified, how would my script invoke the merge tool? – elsevers Apr 27 '14 at 14:20
  • @elsevers the idea would be to *aways* call that wrapper script, and let it worry about which git-supported tool to call. – VonC Apr 27 '14 at 14:23
  • Right -- I am just wondering if there is an easy way for the wrapper script to know how to call the merge tool based on the git configuration settings. For example, the wrapper script could know that .ex1 files should be merged by `mergetool.myex1` and then read `git config mergetool.myex1.cmd` -- allowing a project to override which merge tool is called in its local .gitconfig. This all seems fine when `cmd` is specified -- but what about when only `path` is specified? Maybe the solution is that when using this wrapper script, the config file must always specify `cmd`... – elsevers Apr 27 '14 at 14:44
  • @elsevers note: "read" a config is just get the result of the command "`git config mergetool.myex1.cmd`". If you can detect the extension of the file being merged in one of the arguments of the merge tool wrapper (like `$BASE`), that should be enough to call the right tool. – VonC Apr 27 '14 at 14:46
  • Thank you very much. I have my script running now. – elsevers Apr 27 '14 at 15:37
  • @elsevers Great. Do you have version somewhere (like a gist on GitHub), for me to reference it in the answer? – VonC Apr 27 '14 at 15:38
  • Sorry, I don't... I am a bit new to the online community. If I had a github account, would I upload my script into a gist and then you'd reference it in your answer? – elsevers Apr 27 '14 at 15:54
  • @elsevers that is the idea, yes. – VonC Apr 27 '14 at 15:55
  • @elsevers Excellent. I have included it in the answer for more visilibity. – VonC Apr 27 '14 at 16:11