-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathprepare-commit-msg-jira
More file actions
34 lines (29 loc) · 1.41 KB
/
prepare-commit-msg-jira
File metadata and controls
34 lines (29 loc) · 1.41 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
#!/usr/bin/env bash
# Git Hook for JIRA_TASK_ID
# Adds to the top of your commit message `JIRA_TASK_ID`, based on the prefix of the current branch `feature/AWG-562-add-linter`
# Example: `Add SwiftLint -> `AWG-562 Add SwiftLint
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP='master develop test'
fi
CURRENT_BRANCH=$(git branch --show-current)
for BRANCH in $BRANCHES_TO_SKIP; do
if [ "$BRANCH" = "$CURRENT_BRANCH" ]; then
echo "Info, skipping Jira ID check since current branch is included in the 'BRANCHES_TO_SKIP' variable"
exit 0
fi
done
COMMIT_FILE=$1
COMMIT_MSG=$(cat "$1")
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
JIRA_ID_REGEX="[A-Z0-9]{1,10}-?[A-Z0-9]+"
JIRA_ID_IN_CURRENT_BRANCH_NAME=$(echo "$CURRENT_BRANCH" | { grep -Eo "$JIRA_ID_REGEX" || true; })
JIRA_ID_IN_COMMIT_MESSAGE=$(echo "$COMMIT_MSG" | { grep -Eo "$JIRA_ID_REGEX" || true; })
if [ -n "$JIRA_ID_IN_COMMIT_MESSAGE" ]; then
if [ "$JIRA_ID_IN_COMMIT_MESSAGE" != "$JIRA_ID_IN_CURRENT_BRANCH_NAME" ]; then
echo "Error, your commit message JIRA_TASK_ID='$JIRA_ID_IN_COMMIT_MESSAGE' is not equal to current branch JIRA_TASK_ID='$JIRA_ID_IN_CURRENT_BRANCH_NAME'"
exit 1
fi
elif [ -n "$JIRA_ID_IN_CURRENT_BRANCH_NAME" ]; then
echo "$JIRA_ID_IN_CURRENT_BRANCH_NAME $COMMIT_MSG" > "$COMMIT_FILE"
echo "JIRA ID '$JIRA_ID_IN_CURRENT_BRANCH_NAME', matched in current branch name, prepended to commit message. (Use --no-verify to skip)"
fi