Skip to content

Instantly share code, notes, and snippets.

@artemsky
Last active September 2, 2022 13:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save artemsky/c8f3b8a15292a750656386c59e61db1f to your computer and use it in GitHub Desktop.
Save artemsky/c8f3b8a15292a750656386c59e61db1f to your computer and use it in GitHub Desktop.
Post merge git hook to check Laravel 5+ typical resources were updated
#!/bin/bash
# Looks for changes and automates running bundle and other tasks.
# Does not run if your local branch is behind the remote.
# https://gist.github.com/stefansundin/82051ad2c8565999b914
# post-checkout hook - looks for changes,
# when you change branches, and if found, reinstalls the given packages every
# Exit early if this was only a file checkout, not a branch change ($3 == 1)
[[ $3 == 0 ]] && exit 0
oldRef=$1
newRef=$2
# Exit early if the local branch is behind the remote
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse @{u} 2> /dev/null)
BASE=$(git merge-base @ @{u} 2> /dev/null)
if [[ "$LOCAL" != "$REMOTE" && "$LOCAL" = "$BASE" ]]; then
echo "You are behind origin, not running bundle/migrate post-checkout hook."
exit 1
fi
function changed {
git diff --name-only $oldRef $newRef | grep "^$1" > /dev/null 2>&1
}
if changed 'package.lock'; then
echo "[package.lock] changed: Installing npm dependencies..."
npm install
git add package.json package-lock.json
fi
if changed 'composer.lock'; then
echo "[composer.lock] changed: Installing composer dependencies..."
composer install
fi
if changed 'resources/assets'; then
echo "Front-end sources are changed: Building new dev bundle..."
npm run dev
fi
if changed 'database/migrations'; then
echo "Migrations are changed: Migrating..."
php artisan migrate
fi
if changed 'database/seeds'; then
echo "Seeds are changed: Seeding new data..."
php artisan db:seed
fi
#!/bin/bash
# Looks for changes and automates running bundle and other tasks.
# https://gist.github.com/stefansundin/82051ad2c8565999b914
function changed {
git diff --name-only HEAD@{2} HEAD | grep "^$1" > /dev/null 2>&1
}
if changed 'package.lock'; then
echo "[package.lock] changed: Installing npm dependencies..."
npm install
git add package.json package-lock.json
fi
if changed 'composer.lock'; then
echo "[composer.lock] changed: Installing composer dependencies..."
composer install
fi
if changed 'resources/assets'; then
echo "Front-end sources are changed: Building new dev bundle..."
npm run dev
fi
if changed 'database/migrations'; then
echo "Migrations are changed: Migrating..."
php artisan migrate
fi
if changed 'database/seeds'; then
echo "Seeds are changed: Seeding new data..."
php artisan db:seed
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment