Skip to content

Instantly share code, notes, and snippets.

@bitdivine
Created June 18, 2020 12:17
Show Gist options
  • Save bitdivine/8a6aae39d3cfa529c066baa8d0ddaad7 to your computer and use it in GitHub Desktop.
Save bitdivine/8a6aae39d3cfa529c066baa8d0ddaad7 to your computer and use it in GitHub Desktop.
Conventional commit git hook; enforces the format of commit messages.
#!/bin/bash
set -euo pipefail
COMMIT_MESSAGE="$(head -n1 "$1")"
echo "$COMMIT_MESSAGE" | gawk '
BEGIN{
types["feat"]=0;
types["fix"]=0;
types["doc"]=0;
types["text"]=0;
types["style"]=0;
types["refactor"]=0;
types["process"]=0;
types["revert"]=0;
issue_names["#"]=0;
}
/Merge branch/{exit 0}
{match($0, /^([[:alnum:]]+) ([[:alpha:]#]+)[0-9]+[(][^]]*[)]: .+/, array);
if (RLENGTH == -1) {print "Invalid commit message format:", $0; print " TYPE ISSUE(SCOPE): MESSAGE\ne.g. feat i33(front end): Made a popup" ; exit(1)};
if (!(array[1] in types)) { print "Invalid type:", array[1]; print "Valid types:"; for (key in types) print " ", key; exit 1 }
if (!(array[2] in issue_names)) { print "Invalid issue name:", array[2]; exit 1 }
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment