Skip to content

Instantly share code, notes, and snippets.

@tueda
Last active August 12, 2016 09:29

Revisions

  1. tueda revised this gist Nov 27, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions commit-msg-check.sh
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    #!/bin/sh
    #
    # A (client-side) git hook script which warns if there are too long lines in
    # the commit message.
    # the commit message, not fitting with 50/72 formatting.
    #
    # To use this script, put it as .git/hooks/commit-msg and give it an executable
    # To use this script, copy it as .git/hooks/commit-msg and give it an executable
    # file permission.
    #
    FILE=$1
  2. tueda revised this gist Nov 24, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion commit-msg-check.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/bin/sh
    #
    # A (client-side) git hook script which warns if there are too long lines in
    # the commit messages.
    # the commit message.
    #
    # To use this script, put it as .git/hooks/commit-msg and give it an executable
    # file permission.
  3. tueda created this gist Nov 24, 2013.
    63 changes: 63 additions & 0 deletions commit-msg-check.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    #!/bin/sh
    #
    # A (client-side) git hook script which warns if there are too long lines in
    # the commit messages.
    #
    # To use this script, put it as .git/hooks/commit-msg and give it an executable
    # file permission.
    #
    FILE=$1

    msg_lines() {
    cat "$FILE" \
    | sed 's/^#.*//' \
    | sed '/./=' \
    | sed '/^$/{p;d}; N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /' \
    | sed -n '/./,$p'
    }

    first_line() {
    msg_lines | head -1
    }

    second_line() {
    msg_lines | head -2 | tail -1
    }

    rest_lines() {
    msg_lines | sed '1,2d'
    }

    my_warned=false

    if first_line | sed -n '/\(.\)\{81\}/p' | grep '.' >/dev/null; then
    echo '' >&2
    echo 'WARNING: The first line should be 72 characters or less.' >&2
    first_line >&2
    my_warned=true
    elif first_line | sed -n '/\(.\)\{59\}/p' | grep '.' >/dev/null; then
    echo '' >&2
    echo 'WARNING: The first line is recommended to be 50 characters or less.' >&2
    first_line >&2
    my_warned=true
    fi

    if second_line | grep '.' >/dev/null; then
    echo '' >&2
    echo 'WARNING: The second line should be blank.' >&2
    second_line >&2
    my_warned=true
    fi

    if rest_lines | sed -n '/\(.\)\{81\}/p' | grep '.' >/dev/null; then
    echo '' >&2
    echo 'WARNING: Text should be wrapped at 72 characters.' >&2
    rest_lines | sed -n '/\(.\)\{81\}/p' | grep '.' >&2
    my_warned=true
    fi

    if $my_warned; then
    echo '' >&2
    echo ' use "git commit --amend" etc. to reedit the commit message.' >&2
    echo '' >&2
    fi