Skip to content

Instantly share code, notes, and snippets.

@mortenscheel
Last active September 11, 2024 10:45
Show Gist options
  • Save mortenscheel/9cf1d93b33268e4d1b2172146cbd6e20 to your computer and use it in GitHub Desktop.
Save mortenscheel/9cf1d93b33268e4d1b2172146cbd6e20 to your computer and use it in GitHub Desktop.
Laravel specific git hooks for post-checkout and post-merge
#!/usr/bin/env bash
NC='\033[0m' # No Color
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
GREEN='\033[0;32m'
RED='\033[0;31m'
MAGENTA='\033[0;35m'
notify_about_actions_required() {
changed_files="$(git diff-tree -r --name-status --no-commit-id $1 $2)"
is_changed() {
echo "$changed_files" | grep -Eq "$1"
}
is_added() {
echo "$changed_files" | grep -Eq "^A\s+$1"
}
is_modified() {
echo "$changed_files" | grep -Eq "^M\s+$1"
}
is_deleted() {
echo "$changed_files" | grep -Eq "^D\s+$1"
}
print_notification() {
filename="$1"
shift
action="$@"
echo -e "${YELLOW}${filename}${NC} changed. Please run ${CYAN}${action}${NC}"
}
is_changed composer.lock && print_notification "composer.lock" "composer install"
is_changed package-lock.json && print_notification "package-lock.json" "npm install"
is_changed ^database/seeders && print_notification "Seeders" "php artisan db:seed"
if is_deleted database/migrations; then
echo -e "${YELLOW}Migration files${NC} ${RED}removed${NC}. Consider going back to the previous branch to rollback changes with ${CYAN}php artisan migrate:rollback${NC}"
fi
if is_modified database/migrations; then
echo -e "${YELLOW}Migration files${NC} ${MAGENTA}modified${NC}. Consider running ${CYAN}php artisan migrate:rollback && php artisan migrate${NC}"
fi
if is_added database/migrations; then
echo -e "${YELLOW}Migration files${NC} ${GREEN}added${NC}. Please run ${CYAN}php artisan migrate${NC}"
fi
}
#!/usr/bin/env bash
source "$(dirname $0)/functions.sh"
notify_about_actions_required $1 $2
exit 0
#!/usr/bin/env bash
source "$(dirname $0)/functions.sh"
notify_about_actions_required HEAD@{1} HEAD@{0}
exit 0
@mortenscheel
Copy link
Author

Place all 3 files in .git/hooks in the Laravel project.

@mortenscheel
Copy link
Author

Example output when switching branches
image

@ifox
Copy link

ifox commented May 24, 2022

This is pretty smart! Thanks for sharing! ❤️

@antonioribeiro
Copy link

Yes, so cool! 🔥

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