-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from greg0ire/51--jira-ticket-support
Add in Ticket ref from branch name to each commit
- Loading branch information
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Description | ||
|
||
Adds a ticket reference to the commit-msg, this allows for some workflows to | ||
be able to correlate a number of commits against a specific | ||
feature/ticket/incident/issue etc. once merged. | ||
|
||
A branch of `ABC-1234-hello-world` would turn a commit of | ||
`git commit -m "Hello World"` into `[ABC-1234] Hello World`. (by default) | ||
|
||
# Activation | ||
|
||
```sh | ||
git config --add hooks.enabled-plugins ticketref | ||
``` | ||
|
||
# Configuration | ||
|
||
By default this hook will prefix the commit message with the ticket reference, | ||
this can be configured to be added as a suffix via: | ||
```sh | ||
git config hooks.ticketref.position "POST" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/bash | ||
test -d "$GIT_DIR"/rebase-merge -o -d "$GIT_DIR"/rebase-apply && exit 0 | ||
for enabled_plugin in $(git config --get-all hooks.enabled-plugins) | ||
do | ||
if [ -f "$GIT_DIR/hooks/$enabled_plugin/commit-msg" ] | ||
then | ||
"$GIT_DIR/hooks/$enabled_plugin/commit-msg" "$1" "$2" | ||
fi | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/bin/bash | ||
# shellcheck source=../git_config_wrapper.sh | ||
GIT_DIR=$(git rev-parse --git-dir) | ||
. "$GIT_DIR/hooks/git_config_wrapper.sh" | ||
|
||
get_hook_config ticketref position REF_LOC optional PRE | ||
|
||
# If commit message is a fixup message, ignore it | ||
grep --quiet 'fixup!' "$1" && FIXUP="YES" | ||
|
||
TICKET=$(git symbolic-ref HEAD | rev | cut -d/ -f1 | rev | grep -o -E "[A-Z]+-[0-9]+") | ||
grep --quiet "${TICKET}" "$1" && TICKET_EXISTS="YES" | ||
|
||
if [[ -n "${TICKET}" && -z "${TICKET_EXISTS}" && -z "${FIXUP}" ]]; then | ||
|
||
if [[ "${REF_LOC}" == "PRE" ]]; then | ||
sed -i.bak -e "1s/^/[${TICKET}] /" "$1" | ||
else | ||
printf '\n\nRef: %s' "${TICKET}" >> "$1" | ||
fi | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/bin/sh | ||
|
||
testAddsTicketRefToStartOfCommitMsg() | ||
{ | ||
testRepo=$SHUNIT_TMPDIR/test_repo1 | ||
mkdir -p "${testRepo}" | ||
cd "${testRepo}" || exit 1 | ||
git init --quiet . | ||
git config --add hooks.enabled-plugins ticketref | ||
git checkout -b ABC-1234-branch 2>/dev/null | ||
date > dummyFile | ||
git add dummyFile | ||
git commit --quiet --message "Add dummyFile to repo" | ||
git log -1 | grep 'ABC-1234' | ||
rtrn=$? | ||
assertEquals "ABC-1234 found within Commit " 0 $rtrn | ||
} | ||
|
||
testEnsureCommitMsgContainsOneTicketRef() | ||
{ | ||
testRepo=$SHUNIT_TMPDIR/test_repo2 | ||
mkdir -p "${testRepo}" | ||
cd "${testRepo}" || exit 1 | ||
git init --quiet . | ||
git config hooks.enabled-plugins ticketref | ||
git checkout -b ABC-1234-branch 2>/dev/null | ||
date > dummyFile | ||
git add dummyFile | ||
git commit --quiet --message "ABC-1234 Add dummyFile to repo" | ||
git log -1 | grep -E '^ABC-1234' | ||
rtrn=$? | ||
assertEquals "Single reference of ABC-1234 found within Commit " 1 $rtrn | ||
} | ||
|
||
testRefIsAddedasSuffixToCommit() | ||
{ | ||
testRepo=$SHUNIT_TMPDIR/test_repo3 | ||
mkdir -p "${testRepo}" | ||
cd "${testRepo}" || exit 1 | ||
git init --quiet . | ||
git config hooks.enabled-plugins ticketref | ||
git config hooks.ticketref.position "POST" | ||
git checkout -b ABC-1234-branch 2>/dev/null | ||
date > dummyFile | ||
git add dummyFile | ||
git commit --quiet --message "Add dummyFile to repo" | ||
git log -1 | grep -E 'Ref: ABC-1234$' | ||
rtrn=$? | ||
assertEquals "Reference of ABC-1234 found within Commit at the end " 0 $rtrn | ||
} | ||
|
||
[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0 | ||
. "$(which shunit2)" |