Skip to content

Instantly share code, notes, and snippets.

@markddavidoff
Last active April 16, 2021 05:06
Show Gist options
  • Select an option

  • Save markddavidoff/d9762718ff82c565df38d6deabbe6448 to your computer and use it in GitHub Desktop.

Select an option

Save markddavidoff/d9762718ff82c565df38d6deabbe6448 to your computer and use it in GitHub Desktop.

Revisions

  1. markddavidoff revised this gist Apr 16, 2021. 1 changed file with 18 additions and 7 deletions.
    25 changes: 18 additions & 7 deletions prepare-commit-msg
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,34 @@
    #!/bin/sh
    # commit hook to prepend the JIRA ticket from the branch name to the commit
    # Installation for one repo:
    # - copy this file into your git repo's .git/hooks folder
    # - make sure its executable:
    # chmod a+x repo-path/.git/hooks/prepare-commit-msg
    # Installation for all new repos: (you'll still have to do the above for already cloned repos)
    # - tell git where your global hooks live:
    # git config --global init.templatedir '~/.git-templates'
    # - mkdir -p ~/.git-templates/hooks
    # - copy this file to ~/.git-templates/hooks
    # - make sure its executable:
    # chmod a+x ~/.git-templates/hooks/prepare-commit-msg

    # you can customize which branches should be skipped when
    # prepending commit message.
    if [ -z "$BRANCHES_TO_SKIP" ]; then
    BRANCHES_TO_SKIP=(development)
    BRANCHES_TO_SKIP=(master)
    fi
    BRANCH_NAME=$(git symbolic-ref --short HEAD)
    BRANCH_NAME="${BRANCH_NAME##*/}"

    BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
    TICKET_NUMBER_REGEX="[a-zA-Z][a-zA-Z0-9]*[-][0-9][0-9]*"
    TICKET_NUMBER_REGEX="[a-zA-Z][a-zA-Z2]*[-][0-9][0-9]*"

    if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && [[ $BRANCH_NAME =~ $TICKET_NUMBER_REGEX ]]
    then
    TICKET_NUMBER=$BASH_REMATCH
    TICKET_IN_COMMIT=$(grep -c "\[*$TICKET_NUMBER\]*" $1)
    TICKET_IN_COMMIT=$(grep -c "$TICKET_NUMBER" $1)

    if ! [[ $TICKET_IN_COMMIT -ge 1 ]]
    then
    printf "Prepending branch name to commit message...\n"
    sed -i.bak -e "1s/^/[$TICKET_NUMBER] /" $1;
    sed -i.bak -e "1s/^/$TICKET_NUMBER /" $1;
    fi
    fi
    fi
  2. markddavidoff created this gist Jul 24, 2019.
    23 changes: 23 additions & 0 deletions prepare-commit-msg
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    #!/bin/sh
    # you can customize which branches should be skipped when
    # prepending commit message.
    if [ -z "$BRANCHES_TO_SKIP" ]; then
    BRANCHES_TO_SKIP=(development)
    fi
    BRANCH_NAME=$(git symbolic-ref --short HEAD)
    BRANCH_NAME="${BRANCH_NAME##*/}"

    BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
    TICKET_NUMBER_REGEX="[a-zA-Z][a-zA-Z0-9]*[-][0-9][0-9]*"

    if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && [[ $BRANCH_NAME =~ $TICKET_NUMBER_REGEX ]]
    then
    TICKET_NUMBER=$BASH_REMATCH
    TICKET_IN_COMMIT=$(grep -c "\[*$TICKET_NUMBER\]*" $1)

    if ! [[ $TICKET_IN_COMMIT -ge 1 ]]
    then
    printf "Prepending branch name to commit message...\n"
    sed -i.bak -e "1s/^/[$TICKET_NUMBER] /" $1;
    fi
    fi