Skip to content

Instantly share code, notes, and snippets.

@robatron
Last active April 13, 2023 17:26
Show Gist options
  • Select an option

  • Save robatron/01b9a1061e1e8b35d270 to your computer and use it in GitHub Desktop.

Select an option

Save robatron/01b9a1061e1e8b35d270 to your computer and use it in GitHub Desktop.

Revisions

  1. robatron revised this gist Jan 21, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion auto-prepend-jira-id-to-git-commit-msg.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Automatically Prepend a Jira Issue ID to Git Commit Messages
    > Use a [git hook](https://git-scm.com/docs/githooks) to match a Jira issue ID from the current branch, and prepend it to every commit message
    Assuming the current branch contains a Jira issue ID, you can use the a git hook script to prepend it to every commit message.
    Assuming the current branch contains a Jira issue ID, you can use a git hook script to prepend it to every commit message.

    1. Create an empty *commit-msg* git hook file, and make it executable. From your project's root directory:

  2. robatron revised this gist Jan 21, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion auto-prepend-jira-id-to-git-commit-msg.md
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ fi
    Me too! Here's a one-liner to install the hook above:

    ```bash
    if [ -d .git/hooks ]; then HOOKF=.git/hooks/commit-msg && if [ ! -f $HOOKF ]; then TMPF=`date +%s`-ghook && curl -s https://gist.githubusercontent.com/robatron/01b9a1061e1e8b35d270/raw > $TMPF && STARTL=`grep -n "scriptstart" $TMPF | cut -d: -f1` && ENDL=`grep -n "scriptend" $TMPF | cut -d: -f1` && sed -n `expr \`echo $STARTL\` + 2`,`expr \`echo $ENDL\` - 2`p $TMPF > $HOOKF && chmod 755 $HOOKF; rm $TMPF; else echo "Error: $HOOKF exists"; fi else echo "Error: Not a git repo"; fi
    if [ -d .git/hooks ]; then HOOKF=.git/hooks/commit-msg && if [ ! -f $HOOKF ]; then TMPF=`date +%s`-ghook && curl -s https://gist.githubusercontent.com/robatron/01b9a1061e1e8b35d270/raw > $TMPF && STARTL=`grep -n "scriptstart" $TMPF | head -1 | cut -d: -f1` && ENDL=`grep -n "scriptend" $TMPF | head -1 | cut -d: -f1` && sed -n `expr \`echo $STARTL\` + 2`,`expr \`echo $ENDL\` - 2`p $TMPF > $HOOKF && chmod 755 $HOOKF; rm $TMPF; else echo "Error: $HOOKF exists"; fi else echo "Error: Not a git repo"; fi
    ```

    ## Enable Hooks Globally
  3. robatron revised this gist Jan 21, 2016. 1 changed file with 0 additions and 18 deletions.
    18 changes: 0 additions & 18 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -1,18 +0,0 @@
    if [ -d .git/hooks ]; then
    HOOKF=.git/hooks/commit-msg
    if [ -f $HOOKF ]; then
    TMPF=`date +%s`-ghook
    curl -s https://gist.githubusercontent.com/robatron/01b9a1061e1e8b35d270/raw > $TMPF
    STARTL=`grep -n "scriptstart" $TMPF | cut -d: -f1`
    ENDL=`grep -n "scriptend" $TMPF | cut -d: -f1`
    sed -n `expr \`echo $STARTL\` + 2`,`expr \`echo $ENDL\` - 2`p $TMPF > $HOOKF
    chmod 755 $HOOKF
    rm $TMPF
    else
    echo "Error: $HOOKF exists"
    exit 1
    fi
    else
    echo "Error: Not a git repo"
    exit 1
    fi
  4. robatron revised this gist Jan 21, 2016. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions auto-prepend-jira-id-to-git-commit-msg.md
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,14 @@ fi
    ```
    <!-- scriptend -->

    ## But I'm Lazy

    Me too! Here's a one-liner to install the hook above:

    ```bash
    if [ -d .git/hooks ]; then HOOKF=.git/hooks/commit-msg && if [ ! -f $HOOKF ]; then TMPF=`date +%s`-ghook && curl -s https://gist.githubusercontent.com/robatron/01b9a1061e1e8b35d270/raw > $TMPF && STARTL=`grep -n "scriptstart" $TMPF | cut -d: -f1` && ENDL=`grep -n "scriptend" $TMPF | cut -d: -f1` && sed -n `expr \`echo $STARTL\` + 2`,`expr \`echo $ENDL\` - 2`p $TMPF > $HOOKF && chmod 755 $HOOKF; rm $TMPF; else echo "Error: $HOOKF exists"; fi else echo "Error: Not a git repo"; fi
    ```

    ## Enable Hooks Globally

    To enable git hooks for all new git projects, you can use your own git template directory to build out your hooks. See the following article:
  5. robatron revised this gist Jan 21, 2016. 1 changed file with 8 additions and 6 deletions.
    14 changes: 8 additions & 6 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,18 @@
    if [ ! -d .git/hooks ]; then
    echo "Error: Not a git repo"
    else
    if [ -d .git/hooks ]; then
    HOOKF=.git/hooks/commit-msg
    if [ -f $HOOKF ]; then
    echo "Error: $HOOKF exists"
    else
    TMPF=`date +%s`-ghook
    curl -s https://gist.githubusercontent.com/robatron/01b9a1061e1e8b35d270/raw > $TMPF
    STARTL=`grep -n "scriptstart" $TMPF | cut -d: -f1`
    ENDL=`grep -n "scriptend" $TMPF | cut -d: -f1`
    sed -n `expr \`echo $STARTL\` + 2`,`expr \`echo $ENDL\` - 2`p $TMPF > $HOOKF
    chmod 755 $HOOKF
    rm $TMPF
    else
    echo "Error: $HOOKF exists"
    exit 1
    fi
    fi
    else
    echo "Error: Not a git repo"
    exit 1
    fi
  6. robatron revised this gist Jan 21, 2016. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    if [ ! -d .git/hooks ]; then
    echo "Error: Not a git repo"
    else
    HOOKF=.git/hooks/commit-msg
    if [ -f $HOOKF ]; then
    echo "Error: $HOOKF exists"
    else
    TMPF=`date +%s`-ghook
    curl -s https://gist.githubusercontent.com/robatron/01b9a1061e1e8b35d270/raw > $TMPF
    STARTL=`grep -n "scriptstart" $TMPF | cut -d: -f1`
    ENDL=`grep -n "scriptend" $TMPF | cut -d: -f1`
    sed -n `expr \`echo $STARTL\` + 2`,`expr \`echo $ENDL\` - 2`p $TMPF > $HOOKF
    chmod 755 $HOOKF
    rm $TMPF
    fi
    fi
  7. robatron renamed this gist Jan 21, 2016. 1 changed file with 0 additions and 0 deletions.
  8. robatron revised this gist Jan 20, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,7 @@ Assuming the current branch contains a Jira issue ID, you can use the a git hook

    1. Save the following script to the newly-created *.git/hooks/commit-msg* file:

    <!-- scriptstart -->
    ```bash
    #!/bin/sh

    @@ -22,7 +23,7 @@ if [ ! -z "$JIRA_ID" ]; then
    echo "JIRA ID '$JIRA_ID', matched in current branch name, prepended to commit message. (Use --no-verify to skip)"
    fi
    ```
    <!-- endscript -->
    <!-- scriptend -->

    ## Enable Hooks Globally

  9. robatron revised this gist Jan 20, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,7 @@ if [ ! -z "$JIRA_ID" ]; then
    echo "JIRA ID '$JIRA_ID', matched in current branch name, prepended to commit message. (Use --no-verify to skip)"
    fi
    ```
    <!-- endscript -->

    ## Enable Hooks Globally

  10. robatron revised this gist Jan 20, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ if [ ! -z "$JIRA_ID" ]; then
    fi
    ```

    ## Enable Hook Globally
    ## Enable Hooks Globally

    To enable git hooks for all new git projects, you can use your own git template directory to build out your hooks. See the following article:

  11. robatron revised this gist Jan 20, 2016. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,12 @@ if [ ! -z "$JIRA_ID" ]; then
    fi
    ```

    ## Enable Hook Globally

    To enable git hooks for all new git projects, you can use your own git template directory to build out your hooks. See the following article:

    https://coderwall.com/p/jp7d5q/create-a-global-git-commit-hook

    ## References
    - https://git-scm.com/docs/githooks
    - https://confluence.atlassian.com/display/STASHKB/Integrating+with+custom+JIRA+issue+key
  12. robatron revised this gist Jan 20, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -3,11 +3,11 @@
    Assuming the current branch contains a Jira issue ID, you can use the a git hook script to prepend it to every commit message.

    1. Create a *commit-msg* git hook script, and make it executable. From your project's root directory:
    1. Create an empty *commit-msg* git hook file, and make it executable. From your project's root directory:

    install -b -m 755 /dev/null .git/hooks/commit-msg

    1. Save the following script to the newly-created *.git/hooks/commit-msg*
    1. Save the following script to the newly-created *.git/hooks/commit-msg* file:

    ```bash
    #!/bin/sh
  13. robatron revised this gist Jan 20, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,9 @@
    Assuming the current branch contains a Jira issue ID, you can use the a git hook script to prepend it to every commit message.

    1. Create a *commit-msg* git hook script, and make it executable. (From your project's root directory, you can run `install -b -m 755 /dev/null .git/hooks/commit-msg`)
    1. Create a *commit-msg* git hook script, and make it executable. From your project's root directory:

    install -b -m 755 /dev/null .git/hooks/commit-msg

    1. Save the following script to the newly-created *.git/hooks/commit-msg*

  14. robatron revised this gist Jan 20, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    Assuming the current branch contains a Jira issue ID, you can use the a git hook script to prepend it to every commit message.

    1. Create a *commit-msg* git hook script, and make it executable. (From your project's root directory, run `touch .git/hooks/commit-msg && chmod 755 .git/hooks/commit-msg`.)
    1. Create a *commit-msg* git hook script, and make it executable. (From your project's root directory, you can run `install -b -m 755 /dev/null .git/hooks/commit-msg`)

    1. Save the following script to the newly-created *.git/hooks/commit-msg*

  15. robatron revised this gist Jan 20, 2016. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -3,11 +3,7 @@
    Assuming the current branch contains a Jira issue ID, you can use the a git hook script to prepend it to every commit message.

    1. Create a *commit-msg* git hook script, and make it executable.

    # From your project's root directory
    touch .git/hooks/commit-msg
    chmod 755 .git/hooks/commit-msg
    1. Create a *commit-msg* git hook script, and make it executable. (From your project's root directory, run `touch .git/hooks/commit-msg && chmod 755 .git/hooks/commit-msg`.)

    1. Save the following script to the newly-created *.git/hooks/commit-msg*

  16. robatron revised this gist Jan 20, 2016. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -5,11 +5,9 @@ Assuming the current branch contains a Jira issue ID, you can use the a git hook

    1. Create a *commit-msg* git hook script, and make it executable.

    ```bash
    # From your project's root directory
    touch .git/hooks/commit-msg
    chmod 755 .git/hooks/commit-msg
    ```
    # From your project's root directory
    touch .git/hooks/commit-msg
    chmod 755 .git/hooks/commit-msg

    1. Save the following script to the newly-created *.git/hooks/commit-msg*

  17. robatron revised this gist Jan 20, 2016. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # Automatically Prepend a Jira Issue ID to Git Commit Messages
    > Use a git hook to match a Jira issue ID from the current branch, and prepend it to every commit message
    > Use a [git hook](https://git-scm.com/docs/githooks) to match a Jira issue ID from the current branch, and prepend it to every commit message
    Assuming the current branch contains a Jira issue ID, you can use the a git hook script to prepend it to every commit message.

    @@ -30,5 +30,3 @@ fi
    ## References
    - https://git-scm.com/docs/githooks
    - https://confluence.atlassian.com/display/STASHKB/Integrating+with+custom+JIRA+issue+key
    -
    hello
  18. robatron revised this gist Jan 20, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -29,4 +29,6 @@ fi

    ## References
    - https://git-scm.com/docs/githooks
    - https://confluence.atlassian.com/display/STASHKB/Integrating+with+custom+JIRA+issue+key
    - https://confluence.atlassian.com/display/STASHKB/Integrating+with+custom+JIRA+issue+key
    -
    hello
  19. robatron revised this gist Jan 20, 2016. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,11 @@ Assuming the current branch contains a Jira issue ID, you can use the a git hook

    1. Create a *commit-msg* git hook script, and make it executable.

    # From your project's root directory
    touch .git/hooks/commit-msg
    chmod 755 .git/hooks/commit-msg
    ```bash
    # From your project's root directory
    touch .git/hooks/commit-msg
    chmod 755 .git/hooks/commit-msg
    ```

    1. Save the following script to the newly-created *.git/hooks/commit-msg*

  20. robatron revised this gist Jan 20, 2016. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,15 @@
    # Automatically Prepend a Jira Issue ID to Git Commit Messages
    > Use a git hook to match a Jira issue ID from the current branch, and prepend it to every commit message
    Assuming the current branch contains a Jira issue ID, you can use the following git hook to prepend it to every commit message:
    Assuming the current branch contains a Jira issue ID, you can use the a git hook script to prepend it to every commit message.

    *.git/hooks/commit-msg:*
    1. Create a *commit-msg* git hook script, and make it executable.

    # From your project's root directory
    touch .git/hooks/commit-msg
    chmod 755 .git/hooks/commit-msg

    1. Save the following script to the newly-created *.git/hooks/commit-msg*

    ```bash
    #!/bin/sh
  21. robatron revised this gist Jan 20, 2016. 2 changed files with 6 additions and 12 deletions.
    11 changes: 0 additions & 11 deletions commit-msg
    Original file line number Diff line number Diff line change
    @@ -1,11 +0,0 @@
    #!/bin/sh

    COMMIT_FILE=$1
    COMMIT_MSG=$(cat $1)
    CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
    JIRA_ID=$(echo "$CURRENT_BRANCH" | grep -Eo "[A-Z0-9]{1,10}-?[A-Z0-9]+-\d+")

    if [ ! -z "$JIRA_ID" ]; then
    echo "$JIRA_ID $COMMIT_MSG" > $COMMIT_FILE
    echo "JIRA ID '$JIRA_ID', matched in current branch name, prepended to commit message. (Use --no-verify to skip)"
    fi
    7 changes: 6 additions & 1 deletion git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,9 @@
    # Automatically Prepend a Jira Issue ID to a Git Commit Message
    # Automatically Prepend a Jira Issue ID to Git Commit Messages
    > Use a git hook to match a Jira issue ID from the current branch, and prepend it to every commit message
    Assuming the current branch contains a Jira issue ID, you can use the following git hook to prepend it to every commit message:

    *.git/hooks/commit-msg:*

    ```bash
    #!/bin/sh
  22. robatron revised this gist Jan 20, 2016. 2 changed files with 21 additions and 3 deletions.
    11 changes: 11 additions & 0 deletions commit-msg
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    #!/bin/sh

    COMMIT_FILE=$1
    COMMIT_MSG=$(cat $1)
    CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
    JIRA_ID=$(echo "$CURRENT_BRANCH" | grep -Eo "[A-Z0-9]{1,10}-?[A-Z0-9]+-\d+")

    if [ ! -z "$JIRA_ID" ]; then
    echo "$JIRA_ID $COMMIT_MSG" > $COMMIT_FILE
    echo "JIRA ID '$JIRA_ID', matched in current branch name, prepended to commit message. (Use --no-verify to skip)"
    fi
    13 changes: 10 additions & 3 deletions git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,17 @@
    # Automatically Prepend a Jira Issue ID to a Git Commit Message

    ```bash
    #!/bin/sh

    COMMIT_FILE=$1
    COMMIT_MSG=`cat $1`
    JIRA_ID=`git branch --no-color | grep -E "^\*" | grep -Eo "[A-Z0-9]{1,10}-?[A-Z0-9]+-\d+"}`
    echo "$JIRA_ID $COMMIT_MSG" > $COMMIT_FILE
    COMMIT_MSG=$(cat $1)
    CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
    JIRA_ID=$(echo "$CURRENT_BRANCH" | grep -Eo "[A-Z0-9]{1,10}-?[A-Z0-9]+-\d+")

    if [ ! -z "$JIRA_ID" ]; then
    echo "$JIRA_ID $COMMIT_MSG" > $COMMIT_FILE
    echo "JIRA ID '$JIRA_ID', matched in current branch name, prepended to commit message. (Use --no-verify to skip)"
    fi
    ```

    ## References
  23. robatron revised this gist Jan 20, 2016. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,10 @@
    # Automatically Prepend a Jira Issue ID to a Git Commit Message

    ```bash
    git branch --no-color | grep -E "^\*" | grep -Eo "[A-Z0-9]{1,10}-?[A-Z0-9]+-\d+"
    COMMIT_FILE=$1
    COMMIT_MSG=`cat $1`
    JIRA_ID=`git branch --no-color | grep -E "^\*" | grep -Eo "[A-Z0-9]{1,10}-?[A-Z0-9]+-\d+"}`
    echo "$JIRA_ID $COMMIT_MSG" > $COMMIT_FILE
    ```

    ## References
  24. robatron created this gist Jan 20, 2016.
    9 changes: 9 additions & 0 deletions git-commit-msg-jira-id-prepend.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # Automatically Prepend a Jira Issue ID to a Git Commit Message

    ```bash
    git branch --no-color | grep -E "^\*" | grep -Eo "[A-Z0-9]{1,10}-?[A-Z0-9]+-\d+"
    ```

    ## References
    - https://git-scm.com/docs/githooks
    - https://confluence.atlassian.com/display/STASHKB/Integrating+with+custom+JIRA+issue+key