2
1
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2023-12-13 21:00:40 +01:00
Ghost/.github/hooks/pre-commit
Daniel Lockyer ec9181921b
Fixed preventing git submodules from being committed
- because we now ignore git submodule changes, they didn't show up in
  `git diff --cached...`, so it was possible to get submodules to be
  committed
- you can re-enable submodules to be shown with `--ignore-submodules=none`
- this implements that
- we never want to allow submodules to be committed, so I've removed the
  prompt for faster feedback
2023-03-28 12:16:46 +02:00

45 lines
1.2 KiB
Bash
Executable file

#!/bin/bash
# Modified from https://github.com/chaitanyagupta/gitutils
[ -n "$CI" ] && exit 0
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$GIT_BRANCH" = "main" ]; then
yarn lint-staged --relative
lintStatus=$?
if [ $lintStatus -ne 0 ]; then
echo "❌ Linting failed"
exit 1
fi
fi
green='\033[0;32m'
no_color='\033[0m'
grey='\033[0;90m'
red='\033[0;31m'
ROOT_DIR=$(git rev-parse --show-cdup)
SUBMODULES=$(grep path ${ROOT_DIR}.gitmodules | sed 's/^.*path = //')
MOD_SUBMODULES=$(git diff --cached --name-only --ignore-submodules=none | grep -F "$SUBMODULES")
echo -e "Checking submodules ${grey}(pre-commit hook)${no_color} "
# If no modified submodules, exit with status code 0, else prompt the
# user and exit accordingly
if [[ -n "$MOD_SUBMODULES" ]]; then
echo "Submodules to be committed:"
echo " (use \"git reset HEAD <file>...\" to unstage)"
echo
for SUB in $MOD_SUBMODULES
do
echo -e "\t${green}modified:\t$SUB${no_color}"
done
echo
echo -e "${red}Aborting commit due to submodule update. Please unstage the submodule(s) and commit again.${no_color}"
exit 1
else
echo "No submodules in commit, continuing..."
exit 0
fi