|
| 1 | +#!/bin/sh |
| 2 | +# |
| 3 | +# An example hook script to check the commit log message. |
| 4 | +# Called by "git commit" with one argument, the name of the file |
| 5 | +# that has the commit message. The hook should exit with non-zero |
| 6 | +# status after issuing an appropriate message if it wants to stop the |
| 7 | +# commit. The hook is allowed to edit the commit message file. |
| 8 | +# |
| 9 | +# To enable this hook, rename this file to "commit-msg". |
| 10 | + |
| 11 | +# Uncomment the below to add a Signed-off-by line to the message. |
| 12 | +# Doing this in a hook is a bad idea in general, but the prepare-commit-msg |
| 13 | +# hook is more suited to it. |
| 14 | +# |
| 15 | +# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') |
| 16 | +# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" |
| 17 | + |
| 18 | +# error message function for writing good commit messages |
| 19 | +error_message () { |
| 20 | + cat <<\EOF >&2 |
| 21 | +Error: bad commit message format. A copy of your commit message is in: |
| 22 | +`.git/COMMIT_EDITMSG`. Please fix it or write a new one according to the |
| 23 | +guidlines below. |
| 24 | +
|
| 25 | +The first line of a commit message should start with a capitalized imperative |
| 26 | +verb used in a phrase to summarize the change. This first line must be less |
| 27 | +than 50 characters, followed by an additional blank line. Further detials are |
| 28 | +added after these first two lines, and may be up to 72 characters in length. |
| 29 | +
|
| 30 | +This is so that `--oneline` formatted `git log`s will contain human readable, |
| 31 | +meaningful information, and the summaries on Github.com will also contain |
| 32 | +this concise summary. The 72 line limit is to ensure propper formatting on |
| 33 | +all terminals. |
| 34 | +
|
| 35 | +Here is an example of a good commit message: |
| 36 | +
|
| 37 | +``` |
| 38 | +Redirect user to the requested page after login |
| 39 | +
|
| 40 | +https://github.com/sourceryinstitute/opencoarrays/Issues/29 |
| 41 | +
|
| 42 | +Users were being redirected to the home page after login, which is less |
| 43 | +useful than redirecting to the page they had originally requested before |
| 44 | +being redirected to the login form. |
| 45 | +
|
| 46 | +* Store requested path in a session variable |
| 47 | +* Redirect to the stored location after successfully logging in the user |
| 48 | +* Fixes #1 |
| 49 | +``` |
| 50 | +
|
| 51 | +As opposed to doing something bad like this: |
| 52 | +
|
| 53 | +`git commit -m "Fix login bug"` |
| 54 | +
|
| 55 | +For more information on writting good commit messages, |
| 56 | +and keeping a clean history, please see: |
| 57 | +
|
| 58 | + 1. https://robots.thoughtbot.com/5-useful-tips-for-a-better-commit-message |
| 59 | + 2. http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html |
| 60 | + 3. https://www.reviewboard.org/docs/codebase/dev/git/clean-commits/ |
| 61 | +
|
| 62 | +And for funny examples of what not to do, see: http://whatthecommit.com |
| 63 | +EOF |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +let status=0 |
| 68 | + |
| 69 | +# This example catches duplicate Signed-off-by lines. |
| 70 | + |
| 71 | +test "" = "$(grep '^Signed-off-by: ' "$1" | |
| 72 | + sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { |
| 73 | + echo >&2 "Duplicate Signed-off-by lines." |
| 74 | + let status=1 |
| 75 | +} |
| 76 | + |
| 77 | +# Check that the first line of the commit starts with a |
| 78 | +# capitalized letter. |
| 79 | +if ! (head -n 1 $1 | grep '^[A-Z]' &>/dev/null ); then |
| 80 | + echo >&2 "First word of commit message must be a capitalized imperative verb.\n" |
| 81 | + let status=1 |
| 82 | +fi |
| 83 | + |
| 84 | +# Check each line for propper length |
| 85 | +ln=0 |
| 86 | +cat $1 | \ |
| 87 | + while read line; do |
| 88 | + let ln+=1 |
| 89 | + nchars=$(wc -c <<<$line) |
| 90 | + if [[ "$ln" -eq "1" ]]; then |
| 91 | + if [[ "$nchars" -gt "51" ]]; then |
| 92 | + echo >&2 "First line of commit message too long ($nchars > 50 chars)\n" |
| 93 | + let status=1 |
| 94 | + fi |
| 95 | + elif [[ "$ln" -eq "2" ]]; then |
| 96 | + if [[ "$nchars" -gt "1" ]] && ! grep '^#' <<<"$line" >/dev/null; then |
| 97 | + echo >&2 "Second line of commit message not blank\n" |
| 98 | + let status=1 |
| 99 | + fi |
| 100 | + else |
| 101 | + if [[ "$nchars" -gt "72" ]]; then |
| 102 | + echo >&2 "Line $ln of commit message too long ($nchars > 72 chars)\n" |
| 103 | + let status=1 |
| 104 | + fi |
| 105 | + fi |
| 106 | + done |
| 107 | + |
| 108 | +if [[ $status != 0 ]]; then |
| 109 | + error_message |
| 110 | + exit 1 |
| 111 | +fi |
0 commit comments