Skip to content

Instantly share code, notes, and snippets.

@elasticdog
Created September 20, 2018 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elasticdog/258d51a8c63019e07603bf214bf5c01d to your computer and use it in GitHub Desktop.
Save elasticdog/258d51a8c63019e07603bf214bf5c01d to your computer and use it in GitHub Desktop.
Git pre-commit hook for linting YAML files
#!/usr/bin/env bash
current_branch=$(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')
if [[ $current_branch = 'master' ]]; then
printf 'Direct commits to the master branch are not allowed.\n'
exit 1
fi
if command -v yamllint > /dev/null; then
while IFS= read -r -d '' file; do
if [[ $file =~ \.(yaml|yml)$ ]]; then
if ! yamllint <(git show ":${file}") > /dev/null; then
printf 'YAML syntax check failed for file: %s\n' "$file"
exit 1
fi
fi
done < <(git diff --cached --name-only --diff-filter=ACM -z)
fi
---
extends: default
rules:
braces:
max-spaces-inside: 1
line-length: disable
indentation:
indent-sequences: consistent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment