Repository navigation
Expand file tree
/
Copy pathprepare-commit-message
More file actions
53 lines (43 loc) · 2.3 KB
/
Copy pathprepare-commit-message
File metadata and controls
53 lines (43 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
#
# Inspects branch name and checks if it contains a Jira ticket number (i.e. ABC-123).
# If yes, commit message will be automatically prepended with [ABC-123].
#
# Useful for looking through git history and relating a commit or group of commits
# back to a user story.
#
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
# osascript -e 'display notification "'$BRANCH_NAME'" with title "Sourcetree BRANCH_NAME"'
# Ensure BRANCH_NAME is not empty and is not in a detached HEAD state (i.e. rebase).
# SKIP_PREPARE_COMMIT_MSG may be used as an escape hatch to disable this hook,
# while still allowing other githooks to run.
if [ ! -z "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "HEAD" ] && [ "$SKIP_PREPARE_COMMIT_MSG" != 1 ]; then
PREFIX_PATTERN='[[:print:]]{1,}'
# [[:alnum:]_-/]{1,}
# [A-Z]{2,5}-[0-9]{1,4}
[[ $BRANCH_NAME =~ $PREFIX_PATTERN ]]
PREFIX=${BASH_REMATCH[0]}
# osascript -e 'display notification "'$PREFIX'" with title "Sourcetree PREFIX"'
PREFIX_IN_COMMIT=$(grep -c "\[$PREFIX\]" $1)
# osascript -e 'display notification "'$PREFIX_IN_COMMIT'" with title "Sourcetree PREFIX_IN_COMMIT"'
# Ensure PREFIX exists in BRANCH_NAME and is not already present in the commit message
if [[ -n "$PREFIX" ]] && ! [[ $PREFIX_IN_COMMIT -ge 1 ]]; then
# osascript -e 'display notification "change!!" with title "Sourcetree"'
sed -i.bak -e "1s~^~[$PREFIX] ~" $1
fi
fi
#
# Resources:
# - https://gist.github.com/johncmunson/ca02a8027a923a7f4b2f662c67d6528c
#. - https://gist.github.com/bartoszmajsak/1396344
# - https://stackoverflow.com/questions/34213120/find-branch-name-during-git-rebase
# - https://github.com/typicode/husky/issues/311#issuecomment-580237182
# - https://gmurphey.github.io/2013/02/02/ignoring-git-hooks-when-rebasing.html#.XkK1AhNKjOQ
# - https://mikemadisonweb.github.io/2018/12/18/git-hook-prepending-commit-message/
# - https://stackoverflow.com/questions/5894946/how-to-add-gits-branch-name-to-the-commit-message
# - http://blog.bartoszmajsak.com/blog/2012/11/07/lazy-developers-toolbox-number-1-prepend-git-commit-messages/
# - https://docs.npmjs.com/files/package.json#bin
# - https://www.deadcoderising.com/how-to-smoothly-develop-node-modules-locally-using-npm-link/
# - https://github.com/sindresorhus/execa
# - https://github.com/shelljs/shelljs
#