Skip to content

Instantly share code, notes, and snippets.

@sc0ttkclark
Last active December 1, 2023 10:12
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sc0ttkclark/213fe88f9eacbd5ae58a to your computer and use it in GitHub Desktop.
Save sc0ttkclark/213fe88f9eacbd5ae58a to your computer and use it in GitHub Desktop.
PhpStorm.app (Standalone) Mac integration with Tower.app for diff and merge - https://youtrack.jetbrains.com/issue/WI-26090

PhpStorm.app (Standalone) Mac integration with Tower.app for diff and merge

How to use this

  1. Add CompareTools.plist and phpstorm.sh files in the ~/Library/Application Support/com.fournova.Tower3/CompareTools/ directory. You may need to mkdir ~/Library/Application\ Support/com.fournova.Tower3/CompareTools if the folder does not already exist
  2. Run chmod +x ~/Library/Application\ Support/com.fournova.Tower3/CompareTools/phpstorm.sh
  3. Go into Tower.app Preference > Git Config > Select "PhpStorm" for "Diff tool" and "Compare tool", you can optionally enable "Perform directory diff"

Other variations

Credits

While I went and figured out how to do all of this, this integration is based on the one I found for Beyond Compare's beta integration: https://github.com/tednaleid/git-tower-beyond-compare-shim

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>ApplicationIdentifier</key>
<string>com.jetbrains.PhpStorm</string>
<key>ApplicationName</key>
<string>PhpStorm</string>
<key>DisplayName</key>
<string>PhpStorm</string>
<key>LaunchScript</key>
<string>phpstorm.sh</string>
<key>Identifier</key>
<string>phpstorm</string>
<key>SupportsMergeTool</key>
<true/>
<key>SupportsDiffChangeset</key>
<true/>
</dict>
</array>
</plist>
#! /usr/bin/env bash
# Shell script shim to let PhpStorm.app to integrate with Tower.app
#
# Tower.app gives us these parameters:
LOCAL="$1"
REMOTE="$2"
APPLICATION_PATH=/Applications/PhpStorm.app
CMD="$APPLICATION_PATH/Contents/MacOS/phpstorm"
# Sanitize LOCAL path
if [[ ! "$LOCAL" =~ ^/ ]]; then
LOCAL=$(echo "$LOCAL" | sed -e 's/^\.\///')
LOCAL="$PWD/$LOCAL"
fi
# Sanitize REMOTE path
if [[ ! "$REMOTE" =~ ^/ ]]; then
REMOTE=$(echo "$REMOTE" | sed -e 's/^\.\///')
REMOTE="$PWD/$REMOTE"
fi
MERGING="$4"
BACKUP="/tmp/$(date +"%Y%d%m%H%M%S")"
if [ -n "$MERGING" ]; then
BASE="$3"
MERGE="$4"
# Sanitize BASE path
if [[ ! "$BASE" =~ ^/ ]]; then
BASE=$(echo "$BASE" | sed -e 's/^\.\///')
BASE="$PWD/$BASE"
if [ ! -f "$BASE" ]; then
BASE=/dev/null
fi
fi
# Sanitize MERGE path
if [[ ! "$MERGE" =~ ^/ ]]; then
MERGE=$(echo "$MERGE" | sed -e 's/^\.\///')
MERGE="$PWD/$MERGE"
if [ ! -f "$MERGE" ]; then
# For conflict "Both Added", Git does not pass the merge param correctly in current versions
MERGE=$(echo "$LOCAL" | sed -e 's/\.LOCAL\.[0-9]*//')
fi
fi
sleep 1 # required to create different modification timestamp
touch "$BACKUP"
"$CMD" merge "$LOCAL" "$REMOTE" "$BASE" "$MERGE"
else
"$CMD" diff "$LOCAL" "$REMOTE"
fi
if [ -n "$MERGING" ]; then
# Check if the merged file has changed
if [ "$MERGE" -ot "$BACKUP" ]; then
exit 1
fi
fi
exit 0
@thaicloud
Copy link

This is excellent, thanks!

Worth noting that I did have to create the CompareTools directory, but otherwise works like a charm.

@elliotchance
Copy link

This worked for me. I didn't even need to restart Tower.

It's worth noting that if you are using Tower 3 you must use the correct directory: ~/Library/Application Support/com.fournova.Tower3/CompareTools/

@elliotchance
Copy link

elliotchance commented Feb 4, 2020

If you are using Jetbrains Toolbox the install location will be different. You can find the location, by clicking Tools > PhpStorm > Settings > Install location. Be careful to correctly handle spaces, like:

APPLICATION_PATH="~/Library/Application Support/JetBrains/Toolbox/apps/PhpStorm/ch-0/192.6603.26/PhpStorm.app"

Edit note: Updated the path to remove your username -sc0ttkclark

@sc0ttkclark
Copy link
Author

Thanks @elliotchance, I updated the notes to reflect Tower 3 accordingly!

@Ban117
Copy link

Ban117 commented May 21, 2020

Nice one, works with WebStorm, just needed some small obvious changes.

@sc0ttkclark
Copy link
Author

I keep thinking Tower will get a better diff app built-in but then I remember that PhpStorm does the diff + code highlighting + IDE stuff so it catches a lot of weirdness that I would otherwise not see.

@elliotchance
Copy link

I used Tower for many years. I think all the way back got he first version. Over that time I still tried every other git client (free and paid) and nothing came close to tower.

However, I forced myself to use the git client inside the IDE for a while because of the power of the merge tool giving real context on the source. It took some getting used to but now I love it in the IDE and haven’t used Tower in the last few months.

@sc0ttkclark
Copy link
Author

Updated instructions and made it a little more clear for people

@sc0ttkclark
Copy link
Author

sc0ttkclark commented Jul 29, 2022

Updated with compatibility for Toolbox, here's the full list of supported variations right now:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment