Skip to content

Commit 7402b13

Browse files
authored
Hooks feature (#8)
* TEST: adding gitlab-ci yaml pipeline * TEST: modified readme * TEST: adding success message for git push * TEST: added success and failures logs * TEST: updates in readme * Def1289 rohit (#6) * TEST: making conditional changes in prepare-commit-msg * TEST: added additional logs - DEF1289 * TEST: DEF1289 added additional logs * TEST: removed useless else block - DEF1289 * TEST: - DEF1289 * TEST: sytanx correction and introducing constant for MIN_COMMIT_LENGTH - DEF1289 * TEST: modifying readme * TEST: logs placed for commit abortion * TEST: adding log for compilation start
1 parent 8bc5fca commit 7402b13

File tree

4 files changed

+52
-29
lines changed

4 files changed

+52
-29
lines changed

.githooks/pre-commit

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ else
1313
fi
1414

1515
# Use git diff-index to check for whitespace errors
16-
echo "[pre-commit]: Testing for whitespace errors..."
16+
17+
echo "[pre-commit]: Testing for whitespace errors.................................."
18+
1719
if ! git diff-index --check --cached $against
1820
then
1921
echo "[pre-commit]: commit [ABORTED] due to whitespace errors >---------> [FAILURE]"

.githooks/pre-push

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ CWD=`pwd`
1111

1212
# Move to the project directory which you want to compile
1313

14+
echo '-------------------- MAVEN TEST-COMPILE STARTED -----------------------------'
15+
1416
mvn test-compile
1517
if [ $? -ne 0 ]; then
1618
cd $CWD

.githooks/prepare-commit-msg

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ CURRENT_BRANCH=${arr[0]}
3030
PREFIX_REGEX="((DEF|BAK|STRY|MAINT|def|bak|stry|maint|TNTC)[0-9]*)"
3131
COMMIT_MESSAGE=$(cat $COMMIT_MSG_FILE)
3232
MIN_COMMIT_LENGTH=5
33+
COMMIT_MESSAGE_LENGTH=$(echo -n "$COMMIT_MESSAGE" | wc -c)
3334

34-
if [[ ${#COMMIT_MESSAGE} < $MIN_COMMIT_LENGTH ]]
35+
if [[ $COMMIT_MESSAGE_LENGTH -lt $MIN_COMMIT_LENGTH ]]
3536
then
37+
echo 'COMMIT_MESSAGE_LENGTH = '$COMMIT_MESSAGE_LENGTH
38+
echo 'MIN_COMMIT_LENGTH = '$MIN_COMMIT_LENGTH
3639
echo 'Commit message is too short..... Commit --------------------------> [ABORTED]'
3740
exit 1;
3841
fi
3942

40-
4143
if [[ $CURRENT_BRANCH =~ $PREFIX_REGEX ]]
4244
then
4345
TICKET="${BASH_REMATCH[0]}"

README.md

+43-26
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This project contains a Maven project with tests. Git-Hooks are configured and b
77

88
### Version controlled git-hooks:
99
* ProjectDirectory/.githooks
10-
* <b>pre-commit</b>
10+
>### pre-commit ::
1111
```sh
1212
# ------------------- Check for white-space error -------------------- #
1313
# Check if this is the initial commit
@@ -31,7 +31,7 @@ This project contains a Maven project with tests. Git-Hooks are configured and b
3131
exit 0
3232
fi
3333
```
34-
* <b>prepare-commit-msg</b>
34+
>### prepare-commit-msg ::
3535
```sh
3636
#Append commit message with STRY, DEF, BAK tickets or MAINT
3737
@@ -51,48 +51,65 @@ This project contains a Maven project with tests. Git-Hooks are configured and b
5151
then
5252
#echo "branch name cotains hyphen"
5353
separator="-"
54-
else
55-
separator=""
5654
fi
5755
5856
arr=(${CURRENT_BRANCH//${separator}/ })
5957
CURRENT_BRANCH=${arr[0]}
6058
61-
echo "CURRENT_BRANCH = "${CURRENT_BRANCH} #
59+
# echo "CURRENT_BRANCH = "${CURRENT_BRANCH} #
6260
6361
# PREFIX_REGEX="((DEF|BAK|STRY|MAINT|def|bak|stry|maint|TNTC)[0-9]*)(_*).*"
6462
6563
PREFIX_REGEX="((DEF|BAK|STRY|MAINT|def|bak|stry|maint|TNTC)[0-9]*)"
6664
COMMIT_MESSAGE=$(cat $COMMIT_MSG_FILE)
65+
MIN_COMMIT_LENGTH=5
66+
67+
if [[ ${#COMMIT_MESSAGE} < $MIN_COMMIT_LENGTH ]]
68+
then
69+
echo 'Commit message is too short..... Commit --------------------------> [ABORTED]'
70+
exit 1;
71+
fi
72+
73+
6774
if [[ $CURRENT_BRANCH =~ $PREFIX_REGEX ]]
6875
then
6976
TICKET="${BASH_REMATCH[0]}"
7077
# echo "TICKET IS = "${TICKET}
7178
72-
if [[ $COMMIT_MESSAGE =~ $TICKET ]]
79+
if [[ ! $COMMIT_MESSAGE =~ $TICKET ]]
7380
then
74-
exit 0;
75-
fi
76-
77-
echo "Ticket '${TICKET}', matched in current branch name, appended to commit message."
78-
79-
if [[ $COMMIT_MESSAGE == "TEST"* ]] || [[ $COMMIT_MESSAGE == "test"* ]] || [[ $COMMIT_MESSAGE == "MAINT"* ]] || [[ $COMMIT_MESSAGE == "maint"* ]]
80-
then
81-
echo ${COMMIT_MESSAGE}" - "${TICKET} > $COMMIT_MSG_FILE
81+
echo "Ticket '${TICKET}', matched in current branch name, appended to commit message."
82+
83+
if [[ $COMMIT_MESSAGE == "TEST"* ]] || [[ $COMMIT_MESSAGE == "test"* ]] || [[ $COMMIT_MESSAGE == "MAINT"* ]] || [[ $COMMIT_MESSAGE == "maint"* ]]
84+
then
85+
echo ${COMMIT_MESSAGE}" - "${TICKET} > $COMMIT_MSG_FILE
86+
echo "Ticket '${TICKET}', was appended to the commit message!......................"
87+
echo "Commit Status >------------------------------------------------> [SUCCESSFUL]"
88+
else
89+
echo "TEST: ${COMMIT_MESSAGE} - ${TICKET}" > $COMMIT_MSG_FILE
90+
echo "TEST: was pre-fixed to the commit message...................................."
91+
echo "Commit Status >------------------------------------------------> [SUCCESSFUL]"
92+
fi
8293
else
83-
echo "TEST: ${COMMIT_MESSAGE} - ${TICKET}" > $COMMIT_MSG_FILE
84-
echo "TEST: was pre-fixed to the commit message"
94+
if [[ $COMMIT_MESSAGE != "TEST"* ]] && [[ $COMMIT_MESSAGE != "test"* ]] && [[ $COMMIT_MESSAGE != "MAINT"* ]] && [[ $COMMIT_MESSAGE != "maint"* ]]
95+
then
96+
echo "TEST: ${COMMIT_MESSAGE}" > $COMMIT_MSG_FILE
97+
echo "TEST: was pre-fixed to the commit message...................................."
98+
echo "Commit Status >------------------------------------------------> [SUCCESSFUL]"
99+
fi
85100
fi
86101
87102
elif [[ $COMMIT_MESSAGE == "MAINT"* ]] || [[ $COMMIT_MESSAGE == "TEST"* ]] || [[ $COMMIT_MESSAGE == "test"* ]] || [[ $COMMIT_MESSAGE == "maint"* ]]
88103
then
104+
echo "Commit Status >------------------------------------------------> [SUCCESSFUL]"
89105
exit 0;
90106
else
91107
echo "TEST: "${COMMIT_MESSAGE} > $COMMIT_MSG_FILE
92-
echo "TEST: was pre-fixed to the commit message"
108+
echo "TEST: was pre-fixed to the commit message...................................."
109+
echo "Commit Status >------------------------------------------------> [SUCCESSFUL]"
93110
fi
94111
```
95-
* <b>pre-push</b>
112+
>### pre-push ::
96113
```sh
97114
# ------------------- Compile Maven Project -------------------- #
98115
@@ -116,8 +133,9 @@ This project contains a Maven project with tests. Git-Hooks are configured and b
116133
fi
117134
cd $CWD
118135
```
119-
### Master git-hooks installer:
120-
* ProjectDirectory/install_hooks.sh
136+
137+
>## Master git-hooks installer:
138+
* ProjectDirectory/install_hooks.sh
121139
```sh
122140
#Installs the hooks into your githooks directory
123141
@@ -140,16 +158,15 @@ This project contains a Maven project with tests. Git-Hooks are configured and b
140158
fi
141159
142160
echo "Hooks installed successfully"
143-
144161
```
145162

146-
### Makefile for setups:
147-
* makefile
148-
```makefile
163+
>## Makefile for setups:
164+
* makefile
165+
```makefile
149166
all: say_hello change_hooks_config
150167
# all: say_hello generate_textfiles clean_textfiles
151168
# Note - First Line of the make file is always the default goal/target
152-
# so, it gets executed everytime
169+
# so, it gets executed everytime
153170
say_hello:
154171
echo "Hello World"
155172
@@ -173,7 +190,7 @@ This project contains a Maven project with tests. Git-Hooks are configured and b
173190
change_hooks_config:
174191
echo "Modifying git hook config"
175192
git config core.hooksPath .githooks
176-
```
193+
```
177194

178195
<!-- <p align="center">
179196
<img src="./img/why.png" alt="Statoscope example" width="650">

0 commit comments

Comments
 (0)