Skip to content

Instantly share code, notes, and snippets.

@daryltucker
Last active August 26, 2022 20:55
Show Gist options
  • Save daryltucker/5ec00731d253df123ff795479b62a300 to your computer and use it in GitHub Desktop.
Save daryltucker/5ec00731d253df123ff795479b62a300 to your computer and use it in GitHub Desktop.
Commit Message Hooks for Various Ticketing Systems

Goal

  1. Isolate different git configurations by like-workflows
  2. Automatically associate work tasks to commits.

Example

git checkout features/1234-banana
touch banana; git add banana
git commit -am "Banana"
git show --summary
commit 5d7070b96dd8fd3b1613c1c0c52bd22eb92aaa6a (HEAD -> features/1234-banana)
Author: Daryl Tucker <daryltucker@dontspammebro.com>
Date:   Fri Jul 16 16:20:00 1969 -0700

    DevOps #1234: Banana

Setup

Assuming you create branches in features/ ie (git checkout -b features/1234-banana), one of these commit-msg scripts can be placed into .git/hooks/commit-msg (+x) of each/specific repository directories, however, if you have a setup similar to the following...

~/Projects/
├── WorkflowA
│   ├── repo1
│   └── repo2
└── WorkflowB
    ├── repo3
    └── repo4

... then you can set up a group-specific .gitconfig by sourcing it in your user ~/.gitconfig, and those group-specific configs can reference different git hook scripts:

~/.gitconfig              <---
~/Projects/
├── WorkflowA
│   ├── repo1
│   └── repo2
└── WorkflowB
    ├── .gitconfig        <---
    ├── .githooks
    │   └── commit-msg    <---
    ├── repo3
    └── repo4

WorkflowB/.gitconfig

[user]
    name = Daryl Tucker
    email = daryltucker@dontspammebro.com

[core]
    hooksPath = "~/Projects/WorkflowB/.githooks"

~/.gitconfig

[user]
    name = Daryl Tucker
    email = daryltucker@dontspammebro.com

[includeIf "gitdir:~/Projects/WorkflowB/**"]
    path = "~/Projects/WorkflowB/.gitconfig"

Refs

  1. GitVersion
  2. GitHooks
#!/bin/sh
# Azure DevOps Commit Message Hook
# @daryltucker
# chmod +x .git/hooks/commit-msg
# Skip Rebases
BRANCH_NAME=$(git branch | grep '*' | sed 's/* //')
if [[ $BRANCH_NAME =~ ^\(no\ branch* ]]; then
exit 0
fi
IDENTIFIER=$(git branch | grep '*' | sed 's/* //' | sed -E 's/^feature[s]*\/([0-9]+).*/\1/g')
MESSAGE="$(cat "$1" | sed -e 's/#.*$//')"
if [ -n "$MESSAGE" ] && [ -n "$IDENTIFIER" ]; then
#echo -e "$IDENTIFIER"': '"$(echo "$MESSAGE")" $(echo -E '\n\n')Azure Devops: \#"${IDENTIFIER}" | iconv -t UTF-8 > "$1"
echo -e "DevOps #$IDENTIFIER"': '"$(echo "$MESSAGE")" | iconv -t UTF-8 > "$1"
fi
#!/bin/sh
# JIRA Commit Message Hook
# @daryltucker
# chmod +x .git/hooks/commit-msg
# Skip Rebases
BRANCH_NAME=$(git branch | grep '*' | sed 's/* //')
if [[ $BRANCH_NAME =~ ^\(no\ branch* ]]; then
exit 0
fi
IDENTIFIER=$(git branch | grep '*' | sed 's/* //' | sed -E 's/^feature[s]*/([A-Z a-z]+-[0-9]+).*/\1/g')
MESSAGE="$(cat "$1" | sed -e 's/#.*$//')"
if [ -n "$MESSAGE" ] && [ -n "$IDENTIFIER" ]; then
echo "$IDENTIFIER"': '"$(echo "$MESSAGE")" | iconv -t UTF-8 > "$1"
else
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment