Skip to content

Instantly share code, notes, and snippets.

@ThuggishNuggets
Last active September 29, 2017 22:20
Show Gist options
  • Save ThuggishNuggets/7a2013189e42f52a5bc22a3f66c99c2a to your computer and use it in GitHub Desktop.
Save ThuggishNuggets/7a2013189e42f52a5bc22a3f66c99c2a to your computer and use it in GitHub Desktop.
A post-checkout/post-merge hook for running pod install if necessary after checking out a branch or merging changes

pod install, post-checkout & post-merge

Keep your installed pods up-to-date between branch checkouts! This is a git post-checkout hook for running a pod install if necessary when a checkout event happens in your local working git repository.

Install with

curl https://gist.github.com/ThuggishNuggets/7a2013189e42f52a5bc22a3f66c99c2a/raw/a872173642d9f7c4b0805ef5e76a231155879789/pod-install-post-checkout-hook > .git/hooks/post-checkout
chmod +x .git/hooks/post-checkout

curl https://gist.github.com/ThuggishNuggets/7a2013189e42f52a5bc22a3f66c99c2a/raw/a872173642d9f7c4b0805ef5e76a231155879789/pod-install-post-checkout-hook > .git/hooks/post-merge
chmod +x .git/hooks/post-merge
#!/usr/bin/env bash
from_branch=$1
checked_out_branch=$2
# If checking out a fresh clone
if [ $from_branch = 0000000000000000000000000000000000000000 ]; then
from_branch=$(git hash-object -t tree /dev/null) # a hash representing an empty tree/repo
fi
# If `pod install` does something
if command -v pod install >/dev/null; then
# If Podfile exists and the Podfile has changed
if [ -f Podfile ] && ! git diff $from_branch $checked_out_branch --quiet -- Podfile; then
echo "Podfile changes detected!"
echo "Running 'pod install'..."
export COCOAPODS_DISABLE_STATS=1 # Prevent sending stats, which slows down checkout unbearably
export LANG=en_US.UTF-8 # Cocoapods complain without this
unset -v GIT_DIR # In case git thinks it's somewhere else
/usr/local/bin/pod install
# If Podfile.lock exists and the Podfile has changed
elif [ -f Podfile.lock ] && ! git diff $from_branch $checked_out_branch --quiet -- Podfile.lock; then
echo "Podfile.lock changes detected!"
echo "Running 'pod install'..."
export COCOAPODS_DISABLE_STATS=1 # Prevent sending stats, which slows down checkout unbearably
export LANG=en_US.UTF-8 # Cocoapods complain without this
unset -v GIT_DIR # In case git thinks it's somewhere else
/usr/local/bin/pod install
fi
else
echo "Cocoapods installation was not detected."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment