-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprepare-commit-msg
More file actions
executable file
·47 lines (36 loc) · 1.41 KB
/
prepare-commit-msg
File metadata and controls
executable file
·47 lines (36 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
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/sh
set -e
main() {
# Do nothing during rebase
[ -z "$(git branch --show-current)" ] && exit 0
# Do nothing during amend (without message) or reuse of a commit message
[ "$2" = 'commit' ] && exit 0
#add_jira_issue_id_prefix "$@"
add_co_authored_by_trailers "$@"
}
add_co_authored_by_trailers() {
# Uses https://github.com/Mubashwer/git-mob
trailers=$(git mob --list | sed "s/^/Co-authored-by: /")
[ -n "$trailers" ] || return 0
printf "%s\n" "$trailers" |
sed "s/^/--trailer\n/" |
tr '\n' '\0' |
xargs -0 git interpret-trailers --if-exists addIfDifferent --in-place "$1"
printf "%s\n\n" "$trailers"
}
add_jira_issue_id_prefix() {
# If the branch name starts with string resembling a Jira Issue ID, fetch it
jira_issue_id=$(git branch --show-current | grep -o -E "^[a-zA-Z]+-[0-9]+" | tr '[:lower:]' '[:upper:]')
commit_msg_file_text=$(cat "$1")
commit_msg=$(echo "$commit_msg_file_text" | grep -v "^[[:space:]]*#" || true)
# Skip adding Jira issue ID for fixup and squash commits
if echo "$commit_msg" | grep -q "^fixup!\|^squash!"; then
return 0
fi
# If the Jira Issue ID is identified and the commit message does not already start with it
# then prepend the commit message with it
if [ -n "$jira_issue_id" ] && echo "$commit_msg" | grep -q -i -v "^\[\?$jira_issue_id\]\?"; then
printf "[%s] %s\n" "$jira_issue_id" "$commit_msg_file_text" > "$1"
fi
}
main "$@"