Skip to content

Instantly share code, notes, and snippets.

@Lowess
Forked from bartoszmajsak/prepare-commit-msg.sh
Last active March 4, 2020 10:50

Revisions

  1. Lowess revised this gist Mar 4, 2020. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions prepare-commit-msg.output
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@

    (MLE-297-Fix-naming-for-prometheus) $ ./prepare-commit-msg.sh 'Fix featureA in controller'
    [MLE-297] Fix featureA in controller
  2. Lowess revised this gist Mar 4, 2020. 1 changed file with 36 additions and 7 deletions.
    43 changes: 36 additions & 7 deletions prepare-commit-msg.sh
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,46 @@
    #!/bin/bash

    # This way you can customize which branches should be skipped when
    # prepending commit message.
    if [ -z "$BRANCHES_TO_SKIP" ]; then
    BRANCHES_TO_SKIP=(master develop test)
    # prepending commit message.
    if [ -z "${BRANCHES_TO_SKIP}" ]; then
    BRANCHES_TO_SKIP=(master develop stage test)
    fi

    if [ -z "${ENFORCE_JIRA_PATTERN}" ]; then
    ENFORCE_JIRA_PATTERN=0
    fi

    # Grab the current branch name
    BRANCH_NAME=$(git symbolic-ref --short HEAD)
    # Strip any Gitflow prefix like hotfix/ bugfix/ feature/
    BRANCH_NAME="${BRANCH_NAME##*/}"

    BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
    BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)
    # Attempt to detect Jira project and ticket number format from the branch name
    BRANCH_PROJECT="${BRANCH_NAME%%-*}"
    _BRANCH_REMAINING="${BRANCH_NAME##"${BRANCH_PROJECT}-"}"
    BRANCH_TICKET="${_BRANCH_REMAINING%%-*}"

    JIRA_PREFIX="${BRANCH_PROJECT}-${BRANCH_TICKET}"
    JIRA_PROJECT_REGEX='^[A-Z]{2,10}-[0-9]{1,5}$'

    # Check if the branch is part of excluded ones
    BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -s -c "^$BRANCH_NAME$")
    # Check if the branch name is already in commit
    BRANCH_IN_COMMIT=$(echo "${1}" | grep -c "\\[${BRANCH_NAME}\\]")
    JIRA_IN_COMMIT=$(echo "${1}" | grep -c "\\[${JIRA_PREFIX}\\]")

    if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
    sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1
    if [ -n "${BRANCH_NAME}" ] \
    && ! [[ ${BRANCH_EXCLUDED} -eq 1 ]] \
    && ! [[ ${BRANCH_IN_COMMIT} -ge 1 ]] \
    && ! [[ ${JIRA_IN_COMMIT} -ge 1 ]]; then
    if [[ "${BRANCH_PROJECT}-${BRANCH_TICKET}" =~ ${JIRA_PROJECT_REGEX} ]] ; then
    echo "${1}" | sed -e "s/^/[${JIRA_PREFIX}] /"
    else
    # When JIRA pattern is enforced fail pre-commit on purpose
    if [[ ${ENFORCE_JIRA_PATTERN} -eq 1 ]]; then
    exit 1
    else
    echo "${1}" | sed -e "s/^/[${BRANCH_NAME}] /"
    fi
    fi
    fi
  3. @bartoszmajsak bartoszmajsak revised this gist Oct 15, 2014. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions prepare-commit-msg.sh
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,16 @@

    # This way you can customize which branches should be skipped when
    # prepending commit message.
    DEFAULT_BRANCHES_TO_SKIP=(master develop test)
    if [ -z "$BRANCHES_TO_SKIP" ]; then
    BRANCHES_TO_SKIP=$DEFAULT_BRANCHES_TO_SKIP
    BRANCHES_TO_SKIP=(master develop test)
    fi

    BRANCH_NAME=$(git symbolic-ref --short HEAD)
    BRANCH_NAME="${BRANCH_NAME##*/}"

    if [ -n "$BRANCH_NAME" ] && ! [[ ${BRANCHES_TO_SKIP[*]} =~ $BRANCH_NAME ]]; then
    BRANCH_NAME=${BRANCH_NAME##*/}
    BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
    BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)

    if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
    sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1
    fi
  4. @bartoszmajsak bartoszmajsak revised this gist Sep 22, 2014. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions prepare-commit-msg.sh
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,15 @@
    #!/bin/sh
    #!/bin/bash

    # This way you can customize which branches should be skipped when
    # prepending commit message.
    DEFAULT_BRANCHES_TO_SKIP=(master develop test)
    if [ -z "$BRANCHES_TO_SKIP" ]; then
    BRANCHES_TO_SKIP=("master" "develop" "test")
    fi
    BRANCHES_TO_SKIP=$DEFAULT_BRANCHES_TO_SKIP
    fi

    BRANCH_NAME=$(git symbolic-ref --short HEAD)

    if [ -n "$BRANCH_NAME" ] && ! [[ ${BRANCHES_TO_SKIP[*]} =~ $BRANCH_NAME ]]; then
    BRANCH_NAME=${BRANCH_NAME##*/}
    sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1
    fi
    fi
  5. @bartoszmajsak bartoszmajsak renamed this gist Sep 19, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @bartoszmajsak bartoszmajsak renamed this gist Sep 19, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion prepare-commit-msg.sh → prepare-commit-msg
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    # This way you can customize which branches should be skipped when
    # prepending commit message.
    if [ -z "$BRANCHES_TO_SKIP" ]; then
    BRANCHES_TO_SKIP=("master" "develop" "test")
    BRANCHES_TO_SKIP=("master" "develop" "test")
    fi

    BRANCH_NAME=$(git symbolic-ref --short HEAD)
  7. @bartoszmajsak bartoszmajsak revised this gist Sep 19, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion prepare-commit-msg.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/bin/sh

    # This way you can customize which branches should be skipped when
    # preperending commit message.
    # prepending commit message.
    if [ -z "$BRANCHES_TO_SKIP" ]; then
    BRANCHES_TO_SKIP=("master" "develop" "test")
    fi
  8. @bartoszmajsak bartoszmajsak revised this gist Sep 19, 2014. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions prepare-commit-msg.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,14 @@
    #!/bin/sh

    BRANCH_NAME=$(git symbolic-ref --short HEAD)
    if [ -n "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "master" ]; then
    # This way you can customize which branches should be skipped when
    # preperending commit message.
    if [ -z "$BRANCHES_TO_SKIP" ]; then
    BRANCHES_TO_SKIP=("master" "develop" "test")
    fi

    BRANCH_NAME=$(git symbolic-ref --short HEAD)

    if [ -n "$BRANCH_NAME" ] && ! [[ ${BRANCHES_TO_SKIP[*]} =~ $BRANCH_NAME ]]; then
    BRANCH_NAME=${BRANCH_NAME##*/}
    sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1
    fi
  9. @bartoszmajsak bartoszmajsak revised this gist Sep 18, 2014. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions prepare-commit-msg.sh
    Original file line number Diff line number Diff line change
    @@ -2,5 +2,6 @@

    BRANCH_NAME=$(git symbolic-ref --short HEAD)
    if [ -n "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "master" ]; then
    sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1
    fi
    BRANCH_NAME=${BRANCH_NAME##*/}
    sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1
    fi
  10. @bartoszmajsak bartoszmajsak revised this gist Nov 25, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions prepare-commit-msg.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/sh

    BRANCH_NAME=$(git branch 2>/dev/null | grep -e ^* | tr -d ' *')
    BRANCH_NAME=$(git symbolic-ref --short HEAD)
    if [ -n "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "master" ]; then
    echo "[$BRANCH_NAME] $(cat $1)" > $1
    sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1
    fi
  11. @bartoszmajsak bartoszmajsak renamed this gist Nov 7, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  12. @bartoszmajsak bartoszmajsak revised this gist Dec 13, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions prepare-commit-msg
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/sh

    BRANCH_NAME=$(git branch 2>/dev/null | grep -e ^* | tr -d ' *')
    if [ -n "$BRANCH_NAME" ]; then
    if [ -n "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "master" ]; then
    echo "[$BRANCH_NAME] $(cat $1)" > $1
    fi
    fi
  13. @bartoszmajsak bartoszmajsak created this gist Nov 26, 2011.
    6 changes: 6 additions & 0 deletions prepare-commit-msg
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    #!/bin/sh

    BRANCH_NAME=$(git branch 2>/dev/null | grep -e ^* | tr -d ' *')
    if [ -n "$BRANCH_NAME" ]; then
    echo "[$BRANCH_NAME] $(cat $1)" > $1
    fi