Last active
August 9, 2022 20:37
-
-
Save guywaldman/235530baff33fb3409c19e5e81b5a206 to your computer and use it in GitHub Desktop.
git pre-commit hooks to forbid DNC messages, as seen on https://guywaldman.com/blog/git-hooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Forbidden phrases. | |
FORBIDDEN_PHRASES=("DNC" "DO NOT COMMIT" "TODO(PR)") | |
# ANSI color codes. | |
CLEAR="\033[0m" | |
RED="\033[00;31m" | |
BLUE="\033[00;34m" | |
violation_output="" | |
# Go over the staged changes, and if any of the forbidden phrases is used, | |
# add an appropriate message to the output. | |
for forbidden_word in "${FORBIDDEN_PHRASES[@]}"; do | |
changed_file_names=$(git diff --cached --name-only) | |
for changed_file_name in $changed_file_names; do | |
changed_file_content=$(git diff HEAD --no-ext-diff -U0 --exit-code -a --no-prefix $changed_file_name | egrep "^\+") | |
if echo $changed_file_content | grep -q "$forbidden_word"; then | |
violation_output+="${CLEAR} • ${BLUE}${changed_file_name}${CLEAR} contains ${RED}\"$forbidden_word\"${CLEAR}\n" | |
fi | |
done | |
done | |
# If there are any violations, print the output and exit with an error code. | |
if [[ ! -z $violation_output ]]; then | |
printf "COMMIT REJECTED DNC violation): see below for details\n" | |
printf "$violation_output" | |
exit 1 | |
fi | |
# If there are no violations, exit without an error code. | |
exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env pwsh | |
# Forbidden phrases. | |
$FORBIDDEN_PHRASES = "DNC", "DO NOT COMMIT", "TODO(PR)" | |
$violations = "" | |
# Go over the staged changes, and if any of the forbidden phrases is used, | |
# add an appropriate message to the output. | |
foreach ($forbiddenWord in $FORBIDDEN_PHRASES) { | |
$changedFileNames = "$(git diff --cached --name-only)" | |
foreach ($changedFileName in $changedFileNames) { | |
$changedFileContent = $(git diff HEAD --no-ext-diff --unified=0 --exit-code -a --no-prefix $changedFileName | Select-String "^\+") | |
foreach ($line in $changedFileContent) { | |
if ($line -Match $forbiddenWord) { | |
$violations += "$($PSStyle.Reset) • " | |
$violations += "$($PSStyle.Foreground.Blue)${changedFileName} contains: $($PSStyle.Reset)" | |
$violations += "$($PSStyle.Foreground.Red)${forbiddenWord}$($PSStyle.Reset)" | |
$violations += "`n" # PowerShell newline. Yes... I know ¯\_(ツ)_/¯ | |
} | |
} | |
} | |
} | |
# If there are any violations, print the output and exit with an error code. | |
if ($violations) { | |
Write-Host "COMMIT REJECTED DNC violation): see below for details" | |
Write-Host $violations | |
exit 1 | |
} | |
# If there are no violations, exit without an error code. | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment