Skip to content

Commit 4c0b3bb

Browse files
authored
Merge pull request #4 from rohitkr7/hooksFeature
Hooks feature
2 parents 2fc2a3c + 8960843 commit 4c0b3bb

File tree

6 files changed

+132
-70
lines changed

6 files changed

+132
-70
lines changed

.githooks/pre-commit

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
#!/bin/bash
22

3-
#changes directory to glide-template and runs mvn clean verify
3+
# ------------------- Check for white-space error -------------------- #
44

5-
CWD=`pwd`
6-
# Move to the project directory which you want to build
7-
# cd glide-template
8-
mvn clean verify -Dmaven.test.skip=true
9-
if [ $? -ne 0 ]; then
10-
cd $CWD
11-
exit 1
5+
# Check if this is the initial commit
6+
if git rev-parse --verify HEAD >/dev/null 2>&1
7+
then
8+
echo "pre-commit: About to create a new commit..."
9+
against=HEAD
10+
else
11+
echo "pre-commit: About to create the first commit..."
12+
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
1213
fi
13-
cd $CWD
14+
15+
# Use git diff-index to check for whitespace errors
16+
echo "pre-commit: Testing for whitespace errors..."
17+
if ! git diff-index --check --cached $against
18+
then
19+
echo "pre-commit: Aborting commit due to whitespace errors"
20+
exit 1
21+
else
22+
echo "pre-commit: No whitespace errors :)"
23+
exit 0
24+
fi

.githooks/pre-push

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
# ------------------- Compile Maven Project -------------------- #
4+
5+
#changes directory to glide-template and runs mvn clean verify
6+
7+
CWD=`pwd`
8+
9+
# Get the path of the files which are getting modified
10+
# need to write code
11+
12+
# Move to the project directory which you want to compile
13+
14+
mvn test-compile
15+
if [ $? -ne 0 ]; then
16+
cd $CWD
17+
echo 'Git Push ABORTED because of existing issues, Please check logs!'
18+
exit 1
19+
fi
20+
cd $CWD

.githooks/prepare-commit-msg

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,60 @@
11
#!/bin/bash
22

3-
#Prepends commit message with STRY, DEF, BAK tickets or MAINT
3+
#Append commit message with STRY, DEF, BAK tickets or MAINT
44

55
COMMIT_MSG_FILE=$1
66
COMMIT_SOURCE=$2
77
SHA1=$3
88

99
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
10-
if [ $CURRENT_BRANCH == "HEAD" ]
10+
# echo "CURRENT_BRANCH = "${CURRENT_BRANCH}
11+
12+
separator=""
13+
if [[ $CURRENT_BRANCH == *"_"* ]]
1114
then
12-
exit 0;
15+
#echo "branch name cotains underscore"
16+
separator="_"
17+
elif [[ $CURRENT_BRANCH == *"-"* ]]
18+
then
19+
#echo "branch name cotains hyphen"
20+
separator="-"
21+
else
22+
separator=""
1323
fi
1424

15-
PREFIX_REGEX="((DEF|BAK|STRY|MAINT|def|bak|stry|maint)[0-9]*)"
25+
arr=(${CURRENT_BRANCH//${separator}/ })
26+
CURRENT_BRANCH=${arr[0]}
27+
28+
echo "CURRENT_BRANCH = "${CURRENT_BRANCH} #
29+
30+
# PREFIX_REGEX="((DEF|BAK|STRY|MAINT|def|bak|stry|maint|TNTC)[0-9]*)(_*).*"
31+
32+
PREFIX_REGEX="((DEF|BAK|STRY|MAINT|def|bak|stry|maint|TNTC)[0-9]*)"
33+
COMMIT_MESSAGE=$(cat $COMMIT_MSG_FILE)
1634
if [[ $CURRENT_BRANCH =~ $PREFIX_REGEX ]]
1735
then
1836
TICKET="${BASH_REMATCH[0]}"
19-
COMMIT_MESSAGE=$(cat $COMMIT_MSG_FILE)
37+
# echo "TICKET IS = "${TICKET}
2038

2139
if [[ $COMMIT_MESSAGE =~ $TICKET ]]
2240
then
2341
exit 0;
2442
fi
2543

26-
echo "${TICKET}: ${COMMIT_MESSAGE}" > $COMMIT_MSG_FILE
27-
echo "Ticket '${TICKET}', matched in current branch name, prepended to commit message."
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
49+
else
50+
echo "TEST: ${COMMIT_MESSAGE} - ${TICKET}" > $COMMIT_MSG_FILE
51+
echo "TEST: was pre-fixed to the commit message"
52+
fi
53+
54+
elif [[ $COMMIT_MESSAGE == "MAINT"* ]] || [[ $COMMIT_MESSAGE == "TEST"* ]] || [[ $COMMIT_MESSAGE == "test"* ]] || [[ $COMMIT_MESSAGE == "maint"* ]]
55+
then
56+
exit 0;
57+
else
58+
echo "TEST: "${COMMIT_MESSAGE} > $COMMIT_MSG_FILE
59+
echo "TEST: was pre-fixed to the commit message"
2860
fi

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ clean_textfiles:
1616

1717
build_project:
1818
echo "Building the maven project"
19-
mvn clean verify -Dmaven.test.skip=true
19+
mvn test-compile
2020

2121
install_hooks:
2222
echo "Running install_hooks.sh"

README.md

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,76 +9,75 @@ This project contains a Maven project with tests. Git-Hooks are configured and b
99
* ProjectDirectory/.githooks
1010
* pre-commit
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
20-
fi
21-
cd $CWD
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
20+
fi
21+
cd $CWD
2222
```
2323
* prepare-commit-msg
2424
```sh
25-
#Prepends commit message with STRY, DEF, BAK tickets or MAINT
26-
27-
COMMIT_MSG_FILE=$1
28-
COMMIT_SOURCE=$2
29-
SHA1=$3
30-
31-
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
32-
if [ $CURRENT_BRANCH == "HEAD" ]
25+
#Prepends commit message with STRY, DEF, BAK tickets or MAINT
26+
27+
COMMIT_MSG_FILE=$1
28+
COMMIT_SOURCE=$2
29+
SHA1=$3
30+
31+
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
32+
if [ $CURRENT_BRANCH == "HEAD" ]
33+
then
34+
exit 0;
35+
fi
36+
37+
PREFIX_REGEX="((DEF|BAK|STRY|MAINT|def|bak|stry|maint)[0-9]*)"
38+
if [[ $CURRENT_BRANCH =~ $PREFIX_REGEX ]]
39+
then
40+
TICKET="${BASH_REMATCH[0]}"
41+
COMMIT_MESSAGE=$(cat $COMMIT_MSG_FILE)
42+
43+
if [[ $COMMIT_MESSAGE =~ $TICKET ]]
3344
then
3445
exit 0;
3546
fi
3647
37-
PREFIX_REGEX="((DEF|BAK|STRY|MAINT|def|bak|stry|maint)[0-9]*)"
38-
if [[ $CURRENT_BRANCH =~ $PREFIX_REGEX ]]
39-
then
40-
TICKET="${BASH_REMATCH[0]}"
41-
COMMIT_MESSAGE=$(cat $COMMIT_MSG_FILE)
42-
43-
if [[ $COMMIT_MESSAGE =~ $TICKET ]]
44-
then
45-
exit 0;
46-
fi
47-
48-
echo "${TICKET}: ${COMMIT_MESSAGE}" > $COMMIT_MSG_FILE
49-
echo "Ticket '${TICKET}', matched in current branch name, prepended to commit message."
50-
fi
48+
echo "${TICKET}: ${COMMIT_MESSAGE}" > $COMMIT_MSG_FILE
49+
echo "Ticket '${TICKET}', matched in current branch name, prepended to commit message."
50+
fi
5151
```
5252
### Master git-hooks installer:
5353
* ProjectDirectory/install_hooks.sh
5454
```sh
55-
#Installs the hooks into your githooks directory
56-
57-
printf "This script will copy the following hooks to your .git/hooks directory:\n\n"
58-
for hook in $(ls .githooks)
55+
#Installs the hooks into your githooks directory
56+
57+
printf "This script will copy the following hooks to your .git/hooks directory:\n\n"
58+
for hook in $(ls .githooks)
59+
do
60+
echo $hook
61+
done
62+
printf "\n\n"
63+
read -r -p "Please enter Y to continue: " RESPONSE
64+
65+
if [[ "$RESPONSE" =~ ^([yY][eE][sS]|[yY])$ ]]
66+
then
67+
cd .githooks
68+
for hook in $(ls)
5969
do
60-
echo $hook
70+
cp $hook ../.git/hooks
6171
done
62-
printf "\n\n"
63-
read -r -p "Please enter Y to continue: " RESPONSE
64-
65-
if [[ "$RESPONSE" =~ ^([yY][eE][sS]|[yY])$ ]]
66-
then
67-
cd .githooks
68-
for hook in $(ls)
69-
do
70-
cp $hook ../.git/hooks
71-
done
72-
cd ..
73-
fi
74-
75-
echo "Hooks installed successfully"
72+
cd ..
73+
fi
7674
75+
echo "Hooks installed successfully"
7776
```
7877

7978
### Makefile for setups:
8079
* makefile
81-
```makefile
80+
```Makefile
8281
all: say_hello change_hooks_config
8382
# all: say_hello generate_textfiles clean_textfiles
8483
# Note - First Line of the make file is always the default goal/target
@@ -106,7 +105,7 @@ This project contains a Maven project with tests. Git-Hooks are configured and b
106105
change_hooks_config:
107106
echo "Modifying git hook config"
108107
git config core.hooksPath .githooks
109-
```
108+
```
110109

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

scratchpad.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)