-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit-msg-with-jira
executable file
·47 lines (42 loc) · 1.49 KB
/
commit-msg-with-jira
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
#!/bin/bash
# The script below adds the branch name automatically to
# every one of your commit messages. The regular expression
# below searches for JIRA issue key's. The issue key will
# be extracted out of your branch name
COMMIT_MSG_FILE=$1
COMMIT_MSG=$(cat $COMMIT_MSG_FILE)
REGEX_ISSUE_ID="[A-Z]+\-[0-9]+"
REGEX_ISSUE_BRANCH="[a-zA-Z]+\/[A-Z]+\-[0-9]+"
# Find current branch name
if git rev-parse --git-dir >/dev/null 2>&1; then
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
else
echo "Not in a git repository..."
exit 1
fi
# Check if commit message contains 'no-ticket' and remove it
if [[ $COMMIT_MSG == 'no-ticket'* ]]; then
TRIMMED_COMMIT_MSG=${COMMIT_MSG/#'no-ticket '/}
echo "Commit message contains 'no-ticket'"
echo "$TRIMMED_COMMIT_MSG" >"$1"
exit 0
fi
# Check if commit message already contains issue key
if [[ $COMMIT_MSG =~ $REGEX_ISSUE_ID ]]; then
echo "Commit message already contains issue key. Skipping prepare-commit-msg hook..."
exit 0
fi
# Check if branch name contains issue key
if [[ $BRANCH_NAME =~ $REGEX_ISSUE_BRANCH ]]; then
ISSUE_ID=$(echo "$BRANCH_NAME" | grep -o -E "$REGEX_ISSUE_ID")
if [[ $(cat "$1") == $ISSUE_ID* ]]; then
echo "Commit message already contains issue key. Skipping prepare-commit-msg hook..."
exit 0
fi
echo "Branch name is $BRANCH_NAME. Adding $ISSUE_ID to commit message..."
echo "$ISSUE_ID $(cat "$1")" >"$1"
exit 0
else
echo "Branch name does not contain issue key."
exit 0
fi