|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# This hook verifies that the commit message follows deis commit style |
| 4 | +# To install this hook run the following command from the deis git root |
| 5 | +# cp contrib/util/commit-msg .git/hooks/commit-msg |
| 6 | +set -eo pipefail |
| 7 | + |
| 8 | +RED=$(tput setaf 1) |
| 9 | +NORMAL=$(tput sgr0) |
| 10 | +subject_regex="^(feat|fix|docs|style|ref|test|chore)\(.+\): [\w\s\d]*" |
| 11 | +capital_regex="^.+\): [a-z][\w\s\d]*" |
| 12 | + |
| 13 | +MESSAGE[1]="file" |
| 14 | + |
| 15 | +i=1 # the first array variable is at index 1 |
| 16 | +while read line |
| 17 | +do |
| 18 | + MESSAGE[$i]=$line |
| 19 | + let i++ |
| 20 | +done < "$1" |
| 21 | + |
| 22 | +SUBJECT=${MESSAGE[1]} |
| 23 | + |
| 24 | +if ! [[ $SUBJECT =~ $subject_regex ]]; then |
| 25 | + echo "${RED}ERROR - Invalid subject line." |
| 26 | + echo "" |
| 27 | + echo "$SUBJECT" |
| 28 | + echo "" |
| 29 | + echo "It must be in the format: {type}({scope}): {subject}" |
| 30 | + echo "" |
| 31 | + echo "The following {type}s are allowed:" |
| 32 | + echo "feat" |
| 33 | + echo "fix" |
| 34 | + echo "docs" |
| 35 | + echo "style" |
| 36 | + echo "ref" |
| 37 | + echo "test" |
| 38 | + echo "chore" |
| 39 | + echo "" |
| 40 | + echo "Read more at http://docs.deis.io/en/latest/contributing/standards/$NORMAL" |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | + |
| 44 | +if ! [[ $SUBJECT =~ $capital_regex ]]; then |
| 45 | + echo "${RED}ERROR - Don't the capitalize commit message." |
| 46 | + echo "" |
| 47 | + echo "$SUBJECT" |
| 48 | + echo "" |
| 49 | + echo "Read more at http://docs.deis.io/en/latest/contributing/standards/$NORMAL" |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +if [[ ${#SUBJECT} -gt 50 ]]; then |
| 54 | + echo "${RED}ERROR - Subject can't be longer than 50 characters." |
| 55 | + echo "" |
| 56 | + echo "Read more at http://docs.deis.io/en/latest/contributing/standards/$NORMAL" |
| 57 | + exit 1 |
| 58 | +fi |
| 59 | + |
| 60 | +if [[ ${#MESSAGE[2]} -gt 0 ]]; then |
| 61 | + echo "${RED}ERROR - Second line must be blank" |
| 62 | + echo "" |
| 63 | + echo "Read more at http://docs.deis.io/en/latest/contributing/standards/$NORMAL" |
| 64 | + exit 1 |
| 65 | +fi |
| 66 | + |
| 67 | +cnt=${#MESSAGE[@]} |
| 68 | +for (( i = 3 ; i <= cnt ; i++ )) |
| 69 | +do |
| 70 | + if [[ ${#MESSAGE[$i]} -gt 72 ]]; then |
| 71 | + echo "${RED}ERROR on line $i - can't be longer than 72 characters." |
| 72 | + echo "" |
| 73 | + echo "Read more at http://docs.deis.io/en/latest/contributing/standards/$NORMAL" |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | +done |
| 77 | + |
| 78 | +echo "Your commit message follows the deis commit style" |
| 79 | + |
| 80 | +exit 0 |
0 commit comments