Skip to content

Commit

Permalink
chore: add pre-commit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
shan-shaji committed Oct 6, 2024
1 parent edf1038 commit eef43b4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
60 changes: 60 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env bash

# Initialize variables
BRANCH_DEST=master
FOLDERS_TO_CHECK=("lib" "test")

# Check if dart is available
if command -v dart &> /dev/null
then
DART_COMMAND="dart"
else
echo "Error: Failed to find dart in your path"
exit 1
fi

DART_FORMAT_COMMAND="${DART_COMMAND} format -l 80"

# Initialize empty strings to store files that fail formatting
FILES_NEEDING_FORMAT=""

# Get changed files from git
CHANGED_FILES=$(git diff --name-only origin/${BRANCH_DEST})
GIT_DIFF_EXIT_CODE=$?
if [ $GIT_DIFF_EXIT_CODE -ne 0 ]; then
echo "Error: Failed to execute git diff"
exit $GIT_DIFF_EXIT_CODE
fi

# Exit early if no files have changed
if [ -z "$CHANGED_FILES" ]; then
echo "No files have changed."
exit 0
fi

# Check each changed file for formatting and analysis issues
for FOLDER in "${FOLDERS_TO_CHECK[@]}"
do
for FILE in $(echo "${CHANGED_FILES}" | grep "${FOLDER}.*.dart$")
do
# Check for formatting issues
${DART_FORMAT_COMMAND} -o none --show none --set-exit-if-changed "${FILE}"
FORMAT_EXIT_CODE=$?
if [ $FORMAT_EXIT_CODE -ne 0 ]; then
FILES_NEEDING_FORMAT="${FILES_NEEDING_FORMAT}\n$FILE"
fi
done
done

# Report formatting issues
if [ ! -z "$FILES_NEEDING_FORMAT" ]; then
echo -e "\nThe following files need formatting:\n$FILES_NEEDING_FORMAT\n"
fi

# If any issues are found, exit with error code
if [ ! -z "$FILES_NEEDING_FORMAT" ] ; then
exit 1
else
echo "No formatting or analysis issues found in the changed files."
exit 0
fi
6 changes: 6 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

# Setup pre-commit hooks
git config core.hooksPath .githooks
chmod +x .githooks/pre-commit

0 comments on commit eef43b4

Please sign in to comment.