-
Notifications
You must be signed in to change notification settings - Fork 6
/
prepare-commit-msg
executable file
·101 lines (76 loc) · 2.77 KB
/
prepare-commit-msg
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash -e
COMMIT_FILE=$1
. git-sh-setup
CACHE_FILE=$GIT_DIR/jira.cache
JIRA_SERVER=$(git config jira.server || true)
JIRA_USER=$(git config jira.user || true)
JIRA_PASSWORD=$(git config jira.password || true)
JIRA_COMMIT_TEMPLATE=$(git config jira.commit.template || true)
JIRA_CLI=$(git config jira.cli || true)
JIRA_CMD="$JIRA_CLI --server $JIRA_SERVER --user $JIRA_USER --password $JIRA_PASSWORD"
function prepend () {
local MSG=$1
# Give up if we already have a commit message (like when rebasing)
awk 'NR==1 && /^$/ { exit 0 }; {exit 1} ' < $COMMIT_FILE || return 0
echo -e $MSG | sed -i '' -e "1s/^/\
# [jira]/" -e 'r /dev/stdin' -e '2s/^/# \
/' $COMMIT_FILE
}
function prepend_ok () {
local MSG=$1
# Give up if we already have a commit message (like when rebasing)
awk 'NR==1 && /^$/ { exit 0 }; {exit 1} ' < $COMMIT_FILE || return 0
echo $MSG | sed -i '' -e 'r /dev/stdin' -e '1d' -e '2s/^/\
/' $COMMIT_FILE
}
function get_cached_issue () {
local ISSUE=$1
test -f $CACHE_FILE || return 0
local LINE=`grep ^$ISSUE: $CACHE_FILE`
test -n "$LINE" || return 0
ISSUE_DESCR=$(echo $LINE | sed -e "s/^$ISSUE: //")
}
function fail () {
echo $@
exit 1
}
# start to work
require_work_tree
test -n "$GIT_DIR" || fail "Missing GIT_DIR variable"
test -n "$COMMIT_FILE" || fail "Missing commit file argument"
test -n "$JIRA_SERVER" || fail "Missing git config variable jira.server (set it with 'git config jira.server ...')"
test -n "$JIRA_CLI" || fail "Missing git config variable jira.cli (set it with 'git config jira.cli ...')"
test -n "$JIRA_USER" || fail "Missing git config variable jira.user (set it with 'git config jira.user ...')"
test -n "$JIRA_PASSWORD" || fail "Missing git config variable jira.password (set it with 'git config jira.password ...')"
test -n "$JIRA_COMMIT_TEMPLATE" || JIRA_COMMIT_TEMPLATE="[%i]: %t"
BRANCH_NAME=`git symbolic-ref HEAD | sed -e 's/refs\/heads\///'`
if test -z "$BRANCH_NAME"
then
prepend "# Not committing from a branch"
exit 0
fi
# is the branch a JIRA key?
if [[ ! ( $BRANCH_NAME =~ ^[A-Z]+-[0-9]+$ ) ]]
then
prepend "# Branch does not appear to be a JIRA issue"
exit 0
fi
get_cached_issue $BRANCH_NAME
if test -z "$ISSUE_DESCR"
then
echo "Querying JIRA, this might take a while..."
ISSUE_DESCR=`$JIRA_CMD --action getFieldValue --issue $BRANCH_NAME --field Summary | sed -e '1d'`
SHOULD_CACHE=1
fi
if test -z "$ISSUE_DESCR"
then
prepend "# Branch appears to be a JIRA issue but we were unable to get its description"
exit 0
fi
if test "$SHOULD_CACHE" = "1"
then
echo "$BRANCH_NAME: $ISSUE_DESCR" >> $CACHE_FILE
fi
prepend "# [$BRANCH_NAME]: $ISSUE_DESCR\n# View issue at $JIRA_SERVER/browse/$BRANCH_NAME"
MSG=`echo $JIRA_COMMIT_TEMPLATE | awk -v i="$BRANCH_NAME" -v t="$ISSUE_DESCR" '{sub(/%i/,i); sub(/%t/,t); print}'`
prepend_ok "#$MSG"