Skip to content

Commit 8bc5fca

Browse files
authored
Hooks feature (#7)
* 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
1 parent 7ea673c commit 8bc5fca

File tree

5 files changed

+168
-41
lines changed

5 files changed

+168
-41
lines changed

.githooks/pre-commit

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
# Check if this is the initial commit
66
if git rev-parse --verify HEAD >/dev/null 2>&1
77
then
8-
echo "pre-commit: About to create a new commit..."
8+
echo "[pre-commit]: About to create a new commit..................................."
99
against=HEAD
1010
else
11-
echo "pre-commit: About to create the first commit..."
11+
echo "[pre-commit]: About to create the first commit..............................."
1212
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
1313
fi
1414

1515
# Use git diff-index to check for whitespace errors
16-
echo "pre-commit: Testing for whitespace errors..."
16+
echo "[pre-commit]: Testing for whitespace errors..."
1717
if ! git diff-index --check --cached $against
1818
then
19-
echo "pre-commit: Aborting commit due to whitespace errors"
19+
echo "[pre-commit]: commit [ABORTED] due to whitespace errors >---------> [FAILURE]"
2020
exit 1
2121
else
22-
echo "pre-commit: No whitespace errors :)"
22+
echo "[pre-commit]: No whitespace errors >---------------------------> [SUCCESSFUL]"
2323
exit 0
2424
fi

.githooks/pre-push

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ CWD=`pwd`
1414
mvn test-compile
1515
if [ $? -ne 0 ]; then
1616
cd $CWD
17-
echo 'Git Push ABORTED because of existing issues, Please check logs!'
17+
echo 'Git Push [ABORTED] >-----------> because of existing issues, Please check logs!'
1818
exit 1
19+
else
20+
echo 'Compilation Status: No issues found >--------------------------> [SUCCESSFUL]'
21+
echo 'Git Push >-----------------------------------------------------> [SUCCESSFUL]'
1922
fi
2023
cd $CWD

.githooks/prepare-commit-msg

+31-14
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,60 @@ elif [[ $CURRENT_BRANCH == *"-"* ]]
1818
then
1919
#echo "branch name cotains hyphen"
2020
separator="-"
21-
else
22-
separator=""
2321
fi
2422

2523
arr=(${CURRENT_BRANCH//${separator}/ })
2624
CURRENT_BRANCH=${arr[0]}
2725

28-
echo "CURRENT_BRANCH = "${CURRENT_BRANCH} #
26+
# echo "CURRENT_BRANCH = "${CURRENT_BRANCH} #
2927

3028
# PREFIX_REGEX="((DEF|BAK|STRY|MAINT|def|bak|stry|maint|TNTC)[0-9]*)(_*).*"
3129

3230
PREFIX_REGEX="((DEF|BAK|STRY|MAINT|def|bak|stry|maint|TNTC)[0-9]*)"
3331
COMMIT_MESSAGE=$(cat $COMMIT_MSG_FILE)
32+
MIN_COMMIT_LENGTH=5
33+
34+
if [[ ${#COMMIT_MESSAGE} < $MIN_COMMIT_LENGTH ]]
35+
then
36+
echo 'Commit message is too short..... Commit --------------------------> [ABORTED]'
37+
exit 1;
38+
fi
39+
40+
3441
if [[ $CURRENT_BRANCH =~ $PREFIX_REGEX ]]
3542
then
3643
TICKET="${BASH_REMATCH[0]}"
3744
# echo "TICKET IS = "${TICKET}
3845

39-
if [[ $COMMIT_MESSAGE =~ $TICKET ]]
46+
if [[ ! $COMMIT_MESSAGE =~ $TICKET ]]
4047
then
41-
exit 0;
42-
fi
48+
echo "Ticket '${TICKET}', matched in current branch name, appended to commit message."
4349

44-
echo "Ticket '${TICKET}', matched in current branch name, appended to commit message."
45-
46-
if [[ $COMMIT_MESSAGE == "TEST"* ]] || [[ $COMMIT_MESSAGE == "test"* ]] || [[ $COMMIT_MESSAGE == "MAINT"* ]] || [[ $COMMIT_MESSAGE == "maint"* ]]
47-
then
48-
echo ${COMMIT_MESSAGE}" - "${TICKET} > $COMMIT_MSG_FILE
50+
if [[ $COMMIT_MESSAGE == "TEST"* ]] || [[ $COMMIT_MESSAGE == "test"* ]] || [[ $COMMIT_MESSAGE == "MAINT"* ]] || [[ $COMMIT_MESSAGE == "maint"* ]]
51+
then
52+
echo ${COMMIT_MESSAGE}" - "${TICKET} > $COMMIT_MSG_FILE
53+
echo "Ticket '${TICKET}', was appended to the commit message!......................"
54+
echo "Commit Status >------------------------------------------------> [SUCCESSFUL]"
55+
else
56+
echo "TEST: ${COMMIT_MESSAGE} - ${TICKET}" > $COMMIT_MSG_FILE
57+
echo "TEST: was pre-fixed to the commit message...................................."
58+
echo "Commit Status >------------------------------------------------> [SUCCESSFUL]"
59+
fi
4960
else
50-
echo "TEST: ${COMMIT_MESSAGE} - ${TICKET}" > $COMMIT_MSG_FILE
51-
echo "TEST: was pre-fixed to the commit message"
61+
if [[ $COMMIT_MESSAGE != "TEST"* ]] && [[ $COMMIT_MESSAGE != "test"* ]] && [[ $COMMIT_MESSAGE != "MAINT"* ]] && [[ $COMMIT_MESSAGE != "maint"* ]]
62+
then
63+
echo "TEST: ${COMMIT_MESSAGE}" > $COMMIT_MSG_FILE
64+
echo "TEST: was pre-fixed to the commit message...................................."
65+
echo "Commit Status >------------------------------------------------> [SUCCESSFUL]"
66+
fi
5267
fi
5368

5469
elif [[ $COMMIT_MESSAGE == "MAINT"* ]] || [[ $COMMIT_MESSAGE == "TEST"* ]] || [[ $COMMIT_MESSAGE == "test"* ]] || [[ $COMMIT_MESSAGE == "maint"* ]]
5570
then
71+
echo "Commit Status >------------------------------------------------> [SUCCESSFUL]"
5672
exit 0;
5773
else
5874
echo "TEST: "${COMMIT_MESSAGE} > $COMMIT_MSG_FILE
59-
echo "TEST: was pre-fixed to the commit message"
75+
echo "TEST: was pre-fixed to the commit message...................................."
76+
echo "Commit Status >------------------------------------------------> [SUCCESSFUL]"
6077
fi

.gitlab-ci.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
image: maven:3.6.3-jdk-8
2+
stages:
3+
- build
4+
- test
5+
- deploy
6+
- prod
7+
8+
build:
9+
stage: build
10+
tags:
11+
- local-runner1
12+
script:
13+
- mvn clean install -DskipTests
14+
test:
15+
stage: test
16+
tags:
17+
- local-runner1
18+
script:
19+
- echo 'test'
20+
deploy:
21+
stage: deploy
22+
tags:
23+
- local-runner1
24+
script:
25+
- mvn clean test
26+
when: manual
27+
artifacts:
28+
reports:
29+
junit:
30+
- target/surefire-reports/TEST-*.xml
31+
prod:
32+
stage: prod
33+
tags:
34+
- local-runner1
35+
allow_failure: false
36+
script:
37+
- echo 'prod'
38+
when: manual

README.md

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

88
### Version controlled git-hooks:
99
* ProjectDirectory/.githooks
10-
* pre-commit
10+
* <b>pre-commit</b>
1111
```sh
12-
CWD=`pwd`
13-
# Move to the project directory which you want to build
14-
# cd glide-template
15-
# Build the maven project
16-
mvn clean verify -Dmaven.test.skip=true
17-
if [ $? -ne 0 ]; then
18-
cd $CWD
19-
exit 1
12+
# ------------------- Check for white-space error -------------------- #
13+
# Check if this is the initial commit
14+
if git rev-parse --verify HEAD >/dev/null 2>&1
15+
then
16+
echo "pre-commit: About to create a new commit..."
17+
against=HEAD
18+
else
19+
echo "pre-commit: About to create the first commit..."
20+
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
21+
fi
22+
23+
# Use git diff-index to check for whitespace errors
24+
echo "pre-commit: Testing for whitespace errors..."
25+
if ! git diff-index --check --cached $against
26+
then
27+
echo "pre-commit: Aborting commit due to whitespace errors"
28+
exit 1
29+
else
30+
echo "pre-commit: No whitespace errors :)"
31+
exit 0
2032
fi
21-
cd $CWD
2233
```
23-
* prepare-commit-msg
34+
* <b>prepare-commit-msg</b>
2435
```sh
25-
#Prepends commit message with STRY, DEF, BAK tickets or MAINT
36+
#Append commit message with STRY, DEF, BAK tickets or MAINT
2637
2738
COMMIT_MSG_FILE=$1
2839
COMMIT_SOURCE=$2
2940
SHA1=$3
3041
3142
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
32-
if [ $CURRENT_BRANCH == "HEAD" ]
43+
# echo "CURRENT_BRANCH = "${CURRENT_BRANCH}
44+
45+
separator=""
46+
if [[ $CURRENT_BRANCH == *"_"* ]]
3347
then
34-
exit 0;
48+
#echo "branch name cotains underscore"
49+
separator="_"
50+
elif [[ $CURRENT_BRANCH == *"-"* ]]
51+
then
52+
#echo "branch name cotains hyphen"
53+
separator="-"
54+
else
55+
separator=""
3556
fi
3657
37-
PREFIX_REGEX="((DEF|BAK|STRY|MAINT|def|bak|stry|maint)[0-9]*)"
58+
arr=(${CURRENT_BRANCH//${separator}/ })
59+
CURRENT_BRANCH=${arr[0]}
60+
61+
echo "CURRENT_BRANCH = "${CURRENT_BRANCH} #
62+
63+
# PREFIX_REGEX="((DEF|BAK|STRY|MAINT|def|bak|stry|maint|TNTC)[0-9]*)(_*).*"
64+
65+
PREFIX_REGEX="((DEF|BAK|STRY|MAINT|def|bak|stry|maint|TNTC)[0-9]*)"
66+
COMMIT_MESSAGE=$(cat $COMMIT_MSG_FILE)
3867
if [[ $CURRENT_BRANCH =~ $PREFIX_REGEX ]]
3968
then
4069
TICKET="${BASH_REMATCH[0]}"
41-
COMMIT_MESSAGE=$(cat $COMMIT_MSG_FILE)
70+
# echo "TICKET IS = "${TICKET}
4271
4372
if [[ $COMMIT_MESSAGE =~ $TICKET ]]
4473
then
4574
exit 0;
4675
fi
4776
48-
echo "${TICKET}: ${COMMIT_MESSAGE}" > $COMMIT_MSG_FILE
49-
echo "Ticket '${TICKET}', matched in current branch name, prepended to commit message."
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
82+
else
83+
echo "TEST: ${COMMIT_MESSAGE} - ${TICKET}" > $COMMIT_MSG_FILE
84+
echo "TEST: was pre-fixed to the commit message"
85+
fi
86+
87+
elif [[ $COMMIT_MESSAGE == "MAINT"* ]] || [[ $COMMIT_MESSAGE == "TEST"* ]] || [[ $COMMIT_MESSAGE == "test"* ]] || [[ $COMMIT_MESSAGE == "maint"* ]]
88+
then
89+
exit 0;
90+
else
91+
echo "TEST: "${COMMIT_MESSAGE} > $COMMIT_MSG_FILE
92+
echo "TEST: was pre-fixed to the commit message"
5093
fi
5194
```
95+
* <b>pre-push</b>
96+
```sh
97+
# ------------------- Compile Maven Project -------------------- #
98+
99+
#changes directory to glide-template and runs mvn clean verify
100+
101+
CWD=`pwd`
102+
103+
# Get the path of the files which are getting modified
104+
# need to write code
105+
106+
# Move to the project directory which you want to compile
107+
108+
mvn test-compile
109+
if [ $? -ne 0 ]; then
110+
cd $CWD
111+
echo 'Git Push [ABORTED] >-----------> because of existing issues, Please check logs!'
112+
exit 1
113+
else
114+
echo 'Compilation Status: No issues found >--------------------------> [SUCCESSFUL]'
115+
echo 'Git Push >-----------------------------------------------------> [SUCCESSFUL]'
116+
fi
117+
cd $CWD
118+
```
52119
### Master git-hooks installer:
53120
* ProjectDirectory/install_hooks.sh
54121
```sh
@@ -73,11 +140,12 @@ This project contains a Maven project with tests. Git-Hooks are configured and b
73140
fi
74141
75142
echo "Hooks installed successfully"
143+
76144
```
77145

78146
### Makefile for setups:
79147
* makefile
80-
```Makefile
148+
```makefile
81149
all: say_hello change_hooks_config
82150
# all: say_hello generate_textfiles clean_textfiles
83151
# Note - First Line of the make file is always the default goal/target
@@ -129,7 +197,8 @@ This project contains a Maven project with tests. Git-Hooks are configured and b
129197
3. There is a `Makefile` inside the project directory which is an easy to go step to do all the githooks setups in just one signle shot command which is `make`.
130198

131199
* make file will first of all run ./install_hooks.sh or just set the `git config core.hooksPath`.
132-
* <b>pre-commit</b> has the code to build the project using maven comamnd `mvn clean verify -Dmaven.test.skip=true`
200+
* <b>pre-commit</b> script looks for white-space errors in any of the newly added or modified files.
201+
* <b>pre-push</b> has the code to build the project using maven comamnd `mvn clean verify -Dmaven.test.skip=true` before doing the actual push to the remote repository.
133202
* <b>prepare-commit-msg</b> has code to update commit message according to the branch name. If the branch name contains any defect, story or testcase number in that case the same will be appended to the start of the commit message also.
134203

135204
<!-- <details><summary><b>Show instructions</b></summary>
@@ -144,7 +213,7 @@ This project contains a Maven project with tests. Git-Hooks are configured and b
144213

145214

146215
### Setup
147-
* Fire Below command
216+
* Fire Below command inside the project directory ::
148217
```sh
149218
make
150219
```

0 commit comments

Comments
 (0)