Skip to content

Instantly share code, notes, and snippets.

@josephdpurcell
Forked from aczietlow/prepare-commit-msg
Last active August 29, 2015 14:07

Revisions

  1. josephdpurcell revised this gist Oct 21, 2014. 1 changed file with 12 additions and 4 deletions.
    16 changes: 12 additions & 4 deletions prepare-commit-msg
    Original file line number Diff line number Diff line change
    @@ -28,14 +28,22 @@
    # Exit status of non zero will lead to a failed commit.

    # Get the name of the branch in the current working tree.
    NAME=$(git branch | grep '*' | sed 's/* //')
    NAME=$(git symbolic-ref -q --short HEAD)

    # Check the branch name for a 5 to 7 digit number (an issue number).
    ISSUE_NUMBER=`expr match "$NAME" '\([0-9]\{5,7\}\)'`
    if [ `uname -s` = 'Darwin' ]
    then
    ISSUE_NUMBER=`expr "$NAME" : '\([0-9]*\)'`
    else
    ISSUE_NUMBER=`expr match "$NAME" '\([0-9]\{5,7\}\)'`
    fi

    # If branch contains a match for an issue number prepend the commit
    # message with 'Ref <issue_number> <commit message>.
    if [ -n "$ISSUE_NUMBER" ]
    if [ -n "$ISSUE_NUMBER" ]
    then
    echo "Ref $ISSUE_NUMBER" $(cat "$1") > "$1"
    OLD_IFS=$IFS
    IFS=''
    echo "Ref $ISSUE_NUMBER\n"$(cat "$1") > "$1"
    IFS=$OLD_IFS
    fi
  2. @aczietlow aczietlow created this gist Oct 8, 2014.
    41 changes: 41 additions & 0 deletions prepare-commit-msg
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #!/bin/sh
    #
    # An example hook script to prepare the commit log message.
    # Called by "git commit" with the name of the file that has the
    # commit message, followed by the description of the commit
    # message's source. The hook's purpose is to edit the commit
    # message file. If the hook fails with a non-zero status,
    # the commit is aborted.
    #
    # To enable this hook, rename this file to "prepare-commit-msg" and
    # place it in .git/hooks.
    #
    # To enable this for all git projects uses the global configuration
    # 'init.templatedir'. Create a directory for git templates and a
    # subdirectory for hooks.
    # > mkdir -p ~/.git_template/hooks
    # Use the following command to configure this as your git templates
    # directory.
    # > git config --global init.templatedir '~/.git_template'
    #
    # Now when you init a new git repo, the hook directory along with
    # any hooks will automatically be placed in the new project git
    # repo.
    #
    # You can also run 'git init' on an existing repo to have the
    # reinitialized with the latest templates.
    #
    # Exit status of non zero will lead to a failed commit.

    # Get the name of the branch in the current working tree.
    NAME=$(git branch | grep '*' | sed 's/* //')

    # Check the branch name for a 5 to 7 digit number (an issue number).
    ISSUE_NUMBER=`expr match "$NAME" '\([0-9]\{5,7\}\)'`

    # If branch contains a match for an issue number prepend the commit
    # message with 'Ref <issue_number> <commit message>.
    if [ -n "$ISSUE_NUMBER" ]
    then
    echo "Ref $ISSUE_NUMBER" $(cat "$1") > "$1"
    fi