Skip to content

Instantly share code, notes, and snippets.

@ha1f
Created Jan 25, 2020
Embed
What would you like to do?
#!/bin/sh
echo "Running pre-commit..."
# pre-commit自体が最新かどうかをチェック
PRECOMMIT_MASTER_FILE='./script/pre-commit'
if [ -e $PRECOMMIT_MASTER_FILE ]; then
diff -s $PRECOMMIT_MASTER_FILE ./.git/hooks/pre-commit > /dev/null 2>&1
if [ $? -ne 0 ]; then
cp $PRECOMMIT_MASTER_FILE ./.git/hooks/pre-commit
echo "updated pre-commit."
echo "pre-commit was not latest. Please run again." 1>&2
exit 1
fi
else
echo "$PRECOMMIT_MASTER_FILE doesn't exist."
fi
# 最新コミットをagainstに保存
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Redirect output to stderr.
exec 1>&2
# master, develop branchでのcommitを禁止
BRANCH_NAME=`git symbolic-ref HEAD | sed -e 's:^refs/heads/::'`
if test $BRANCH_NAME = master -o $BRANCH_NAME = develop; then
echo "We cannot commit on ${BRANCH_NAME} branch."
exit 1
fi
# register_devicesをvalidate
for FILE in `git diff-index --name-status $against | grep -E 'fastlane/register_devices.txt' | cut -c3-`; do
for row in `cat fastlane/register_devices.txt | awk -F '\t' '{print NF}'`; do
if [ $row != "2" ]; then
echo "Error: lines in fastlane/register_devices.txt must be separated with tab character."
exit 1
fi
done
done
# CircleCIのconfigをvalidate
for FILE in `git diff-index --name-status $against | grep -E '.circleci/config.yml' | cut -c3-`; do
if ! eMSG=$(circleci config validate -c .circleci/config.yml); then
echo "Error: CircleCI Configuration is invalid."
echo $eMSG
exit 1
fi
done
# コンフリクト未解消ファイルがあったら警告(swift only)
for FILE in `git diff-index --name-status $against | grep -E '\.(swift|pbxproj|m|h|xib|storyboard)$' | cut -c3-`; do
if [ -e $FILE ]; then
# 削除されたファイルは飛ばす
continue
fi
grep_result=`grep -E '(<<<<<<<|>>>>>>>)' $FILE | grep -v '^$'`
if [ -n "${grep_result}" ]; then
echo $'\e[1;31m'$FILE$'\e[m' ' <- コンフリクト未解消ファイルがあります'
echo $grep_result
exit 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment