Skip to content

Instantly share code, notes, and snippets.

@danieltmbr
Created September 16, 2018 22:47
Show Gist options
  • Save danieltmbr/3edbd5d5f340cee35a6d4f502522b198 to your computer and use it in GitHub Desktop.
Save danieltmbr/3edbd5d5f340cee35a6d4f502522b198 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Define colors & styles for outputs
# ----------------------------------
red='\033[1;31m'
green='\033[32m'
bold='\033[1m'
italic='\033[2;3m'
reset='\033[0m'
# Define early exit function
# --------------------------
abort() {
echo -e "${italic}Use ${reset}${bold}'git push --no-verify'${reset}${italic} to bypass linter${reset}"
exit 1
}
# Check for uncommited changes
# ----------------------------
if [[ $(git diff) ]]; then
echo -e "${red}Error${reset}: There are some ${bold}unstaged${reset} changes."
abort
elif [[ $(git diff --cached) ]]; then
echo -e "${red}Error${reset}: There are some ${bold}uncommited${reset} changes."
abort
fi
# Correct changed .swift files if there's any
# -------------------------------------------
# TODO: make this work without echoing error
# changed_files=$((git diff @{upstream} --name-only || git diff origin/dev HEAD --name-only) | grep -Ev 'Pods/' | grep .swift$)
# Let's assume we have a origin/dev branch
git fetch origin
changed_files=$(git diff origin/dev HEAD --name-only | grep -Ev 'Pods/' | grep .swift$)
Pods/SwiftLint/swiftlint autocorrect [$changed_files] --quiet
# Print corrected files
# ---------------------
corrected_files=$(git diff --name-only)
if [ -n "$corrected_files" ]; then
echo -e "SwiftLint ${green}autocorrected${reset} the following files:\n${italic}${corrected_files}${reset}"
git commit -am "SwiftLint autocorrection - pre-push hook"
echo -e "Please ${bold}push${reset} again."
abort
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment