;dev: bin: commitlint: also work a git commit-msg hook
This commit is contained in:
		
							parent
							
								
									215e90ad78
								
							
						
					
					
						commit
						103813f116
					
				| @ -1,21 +1,23 @@ | |||||||
| #!/usr/bin/env bash | #!/usr/bin/env bash | ||||||
| # shellcheck disable=SC2059 |  | ||||||
| # | # | ||||||
| # Check git commits for compliance with hledger's conventions, | # commitlint [GITRANGE|FILE] | ||||||
| # described at https://hledger.org/CONTRIBUTING.html#commit-messages. | 
 | ||||||
| # | # Check unpushed commits, or commits in the specified range, or a | ||||||
| # Usage: | # commit message in FILE, for compliance with hledger's conventions | ||||||
| # commitlint [GITREV|GITRANGE] | # (https://hledger.org/CONTRIBUTING.html#commit-messages). If the | ||||||
| #   Checks unpushed commits, or the specified commit or range. | # argument contains - or .. or ^! it's a range, otherwise a file | ||||||
| #   If the argument contains - or .. it's a range, otherwise a single commit. | # containing a proposed commit message.  | ||||||
|  | # Run interactively, or symlink as .git/hooks/commit-msg to check | ||||||
|  | # your messages before commit. | ||||||
| # | # | ||||||
| # Examples: | # Examples: | ||||||
| # commitlint                              # unpushed commits | # commitlint foo                          # commit message in ./foo | ||||||
| # commitlint HEAD                         # last commit | # commitlint HEAD^!                       # last commit | ||||||
| # commitlint d5d19f841                    # this commit | # commitlint d5d19f841^!                  # this commit | ||||||
| # commitlint -20                          # last 20 commits | # commitlint -20                          # last 20 commits | ||||||
| # commitlint master..                     # commits in this branch | # commitlint master..                     # commits in this branch | ||||||
| # commitlint 1.21..1.22 | grep -F [FAIL]  # bad commits in 1.22 release cycle | # commitlint 1.21..1.22 | grep -F [FAIL]  # bad commits in 1.22 release cycle | ||||||
|  | # commitlint                              # unpushed commits | ||||||
| 
 | 
 | ||||||
| if [[ -n ${NO_COLOR+set} ]] | if [[ -n ${NO_COLOR+set} ]] | ||||||
| then | then | ||||||
| @ -32,39 +34,54 @@ fi | |||||||
| function checkcommit() | function checkcommit() | ||||||
| { | { | ||||||
|     HASH=${1} |     HASH=${1} | ||||||
|     SUMMARY=$(git log -1 "$HASH" --pretty=format:"%s") |     MSG=$(git log -1 "$HASH" --pretty=format:"%s%n%b") | ||||||
|  |     checkmsg "$MSG" | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # checkmsg MSG [GITHASH] - check this commit message and print result | ||||||
|  | function checkmsg() | ||||||
|  | { | ||||||
|  |     MSG=${1} | ||||||
|  |     HASH=${2} | ||||||
|  |     if [[ -n $HASH ]] | ||||||
|  |     then | ||||||
|  |         HASH="$HASH " | ||||||
|  |     fi | ||||||
|  | 
 | ||||||
|  |     SUMMARY=$(echo "$MSG" | head -1) | ||||||
|  |     FMT="%s%-60s  %b${NRM}\n" | ||||||
| 
 | 
 | ||||||
|     # Does the summary have a type: prefix ? |     # Does the summary have a type: prefix ? | ||||||
|     # Can begin with ; and/or end with !, some spaces are tolerated. |     # Can begin with ; and/or end with !, some spaces are tolerated. | ||||||
|     FMT="%s %-60s  %b${NRM}\n" |  | ||||||
|     if ! echo "$SUMMARY" | grep -qE '^(; *)?\w+:[ \w]+!?' |     if ! echo "$SUMMARY" | grep -qE '^(; *)?\w+:[ \w]+!?' | ||||||
|     then |     then | ||||||
|  |         # shellcheck disable=SC2059 | ||||||
|         printf "$FMT" "$HASH" "$SUMMARY" "${RED}[FAIL]" |         printf "$FMT" "$HASH" "$SUMMARY" "${RED}[FAIL]" | ||||||
|         STATUS=1 |         STATUS=1 | ||||||
|     else |     else | ||||||
|  |         # shellcheck disable=SC2059 | ||||||
|         printf "$FMT" "$HASH" "$SUMMARY" "${GRN}[ok]" |         printf "$FMT" "$HASH" "$SUMMARY" "${GRN}[ok]" | ||||||
|     fi |     fi | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | STATUS= | ||||||
| RANGE=${*:-"@{u}.."}  # @{u} = upstream | RANGE=${*:-"@{u}.."}  # @{u} = upstream | ||||||
| 
 | 
 | ||||||
| if [[ ! $RANGE =~ (-|\.\.) ]] | if [[ $RANGE =~ (-|\.\.|\^!) ]] | ||||||
| then | then | ||||||
|     RANGE=$RANGE^!  # assume range is a single commit |  | ||||||
| fi |  | ||||||
| 
 |  | ||||||
|     HASHES=$(git log --abbrev-commit --pretty=format:%h "$RANGE") |     HASHES=$(git log --abbrev-commit --pretty=format:%h "$RANGE") | ||||||
| 
 |     for HASH in $HASHES; do checkcommit "$HASH"; done | ||||||
| STATUS= | else | ||||||
| for HASH in $HASHES; do |     MSG=$(cat "$1") | ||||||
|     checkcommit "$HASH" |     checkmsg "$MSG" | ||||||
| done | fi | ||||||
| 
 | 
 | ||||||
| if [[ -z $STATUS ]] | if [[ -z $STATUS ]] | ||||||
| then | then | ||||||
|     printf "" # "${GRN}Ok${NRM}\n" |     printf "" # "${GRN}Ok${NRM}\n" | ||||||
| else | else | ||||||
|     printf "\n${RED}Some commits are not in preferred style.${NRM}\n" |     # shellcheck disable=SC2059 | ||||||
|  |     printf "\n${RED}Commit(s) not in preferred style.${NRM}\n" | ||||||
|     cat <<EOF |     cat <<EOF | ||||||
| --------------------------------------------------------------------------- | --------------------------------------------------------------------------- | ||||||
| Commit messages should follow this format: | Commit messages should follow this format: | ||||||
| @ -80,15 +97,16 @@ The subject line should have a type: prefix. Common types: | |||||||
|  cha pkg lib             - packager/builder/lib-user changes (->changelogs) |  cha pkg lib             - packager/builder/lib-user changes (->changelogs) | ||||||
|  dev doc test ci ref cln - developer changes |  dev doc test ci ref cln - developer changes | ||||||
| 
 | 
 | ||||||
| It may additionally have a topic prefix such as: | It may additionally have a topic(s) prefix such as: | ||||||
|  |  bin examples install cli ui web print reg bal bs balcmds journal csv ... | ||||||
|  (see https://hledger.org/CONTRIBUTING.html#open-issues -> COMPONENT) |  (see https://hledger.org/CONTRIBUTING.html#open-issues -> COMPONENT) | ||||||
| 
 | 
 | ||||||
| Mention any related issues, usually parenthesised at end of summary: (#1234) | Mention any related issues, usually parenthesised at end of summary: (#1234) | ||||||
| ! indicates a breaking change. | ! indicates a breaking change. | ||||||
| ; skips expensive CI tests. | ; skips expensive CI tests. | ||||||
| 
 | 
 | ||||||
| These conventions are evolving. In practice, any type or topic will | These conventions are evolving. In practice, any type or topic will do. | ||||||
| do. Use your best judgement and we'll polish during code review. | Use your best judgement and we'll polish during code review. | ||||||
| More detail: https://hledger.org/CONTRIBUTING.html#commit-messages | More detail: https://hledger.org/CONTRIBUTING.html#commit-messages | ||||||
| --------------------------------------------------------------------------- | --------------------------------------------------------------------------- | ||||||
| EOF | EOF | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user