-
-
Save Finkregh/2f5529a1701ab401451d80d852028884 to your computer and use it in GitHub Desktop.
Randomly rejects commits. https://twitter.com/mfeathers/status/963587422507421698
This file contains 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
#!/bin/bash | |
# Make this executable and name it .git/hooks/pre-commit to have commits | |
# randomly rejected with a prompt to rethink it. The odds are higher for | |
# longer commits | |
set -eu | |
# How many lines a commit must have before it's considered long and should | |
# have the highest odds of getting an extra prompt | |
LONG_COMMIT_THRESHOLD=150 | |
# The percentage of long commits that will be prompted | |
LONG_COMMIT_PROMPT_ODDS=50 | |
# The percentage of short commits that will be prompted | |
SHORT_COMMIT_PROMPT_ODDS=10 | |
rough_line_count=$( | |
git diff --cached \ | |
| grep -E '^[+-]' \ | |
| wc -l | |
) | |
if [ $rough_line_count -gt $LONG_COMMIT_THRESHOLD ]; then | |
rough_line_count=$LONG_COMMIT_THRESHOLD | |
fi | |
# $RANDOM is in the range 0 - 32768, thus assuming odds of 50%/10% and long commit | |
# threshold of 150 we want to scale the line count so that [0, 150] -> [3276, 16384] | |
high_threshold="$(( 32768*$LONG_COMMIT_PROMPT_ODDS/100 ))" | |
low_threshold="$(( 32768*$SHORT_COMMIT_PROMPT_ODDS/100 ))" | |
scale_factor="$(( ($high_threshold - $low_threshold)/$LONG_COMMIT_THRESHOLD ))" | |
datum="$(( $rough_line_count*$scale_factor + $low_threshold ))" | |
if [ $RANDOM -lt $datum ]; then | |
echo >&2 'Please think a bit more about this.' | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment