How to setup git to allow different merge/diff tools based on file extension.
I'm going to put this here so I can find it later...
Create a merge-wrapper script
To allow us the flexibility we want to determine which diff tool should be used for different file extensions, we need to break out the logic in to an external script. Below I have 2 samples of a merge script. I started with the .sh
file (bottom), but changed over to the .js
version (above that) since it is easier for me to maintain.
I haven't yet tried to run this on windows, but suspect we can wrap the
merge-wrapper.js
in a.cmd
file calling it with Node.JS.
Customize the merge-wrapper.js
-
Extend with other diff tool support:
If you want to extend the script to add support for your own diff tool, just create a new function that returns an object following the pattern of the existing
createP4MergeCommand
orcreateOpenDiffCommand
. -
Modify which diff tool is used per extension:
If you want to change which tool is used per file extensions you can modify the
diffLookup
hash to map various extensions to whatever tool you setup. -
Leave me a comment (either here - or in the gist) of what diff tool you added.
I'd be happy to take contributions of other diff tools in this gist if you leave a comment with yours...
Below was an attempt at using a bash script to manage what I do above, it'll work for some, but I didn't want to maintain this - prefer the JS version instead.
Setup ~/.gitconfig
Now that we've created our merge wrapper script we need to tell git how to use it.
Say we placed our merge script in the following directory: $HOME/dotfiles/tools/merge-wrapper.js
. You can add the below to your ~/.gitconfig
file and when you use git difftool
our new merge-wrapper will be used to pick diff tools based on file extension.
[merge]
tool = merge_wrapper
[mergetool "merge_wrapper"]
cmd = $HOME/dotfiles/tools/merge-wrapper.js \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"