forked from ajm01/boost-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit.template
executable file
·126 lines (107 loc) · 4.21 KB
/
pre-commit.template
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/sh
#
# PreReq's
# - This requires that a current level of Eclipse is defined on your machine
# This hook will run the eclipse code formatter before any commit.
# This will format Java code files that are part of the commit.
# The java code will be formatted based on the preferences defined
# in the java.prefs file provided in the base directory of this project.
#
# Note: this script requires a linux environment to run (linux/MacOSX/cygwin)
# this will not execute in a native Windows environment.
#
# WINDOWS USERS NOTE: Update your eclipse.ini file and remove
# the property -Dosgi.dataAreaRequiresExplicitInit=true
#
# Update the following items below then save this file in
# .git/hooks/pre-commit
# be sure you have execute authority for this file.
# set this to the location of your Eclipse installation
# linux/Mac Example: ECLIPSE_HOME=/Users/mac/EclipseInstalls/EclipseO2/EClipse.app/Contents/MacOS
# Windows/Cygwin Example: ECLIPSE_HOME="C:/EclJavaFrmtAdmin/eclipse"
ECLIPSE_HOME=***** Fully qualified location of your Eclipse executable *****
# Style file is provided in the root directory of the project as java.prefs
PROJECT_LOCATION=$PWD
STYLE_FILE=$PROJECT_LOCATION/java.prefs
echo "Running pre-commit hook: run-eclipse-formatter---------------------"
echo "Will run eclipse formatter, using: $STYLE_FILE"
statusFile=status.$RANDOM
fileCommand="git status --porcelain 2>&1 > $statusFile"
$(eval $fileCommand)
newlist=Y
while read -r line
do
#Check to see if this is a java file
if echo "$line" | grep -q "\.java"; then
actionCmd="echo \"$line\" | cut -c1-2"
action=$(eval $actionCmd)
# Is the file being renamed?
if [ "$action" == "RM" ] || [ "$action" == "R " ] ; then
echo "File $line is being renamed"
# find the new file name
newFileCmd="echo \"$line\" | sed 's/.*\=> //'"
newFile=$(eval $newFileCmd)
if [ $newlist == "Y" ]] ; then
# formatFiles are input to the eclipse formatter
formatFiles=$PROJECT_LOCATION/$newFile
#
checkIfFormatted=$newFile
newlist=N
else
formatFiles="$formatFiles $PROJECT_LOCATION/$newFile"
checkIfFormatted="$checkIfFormatted $newFile"
fi
elif [ "$action" == "MD" ] || [ "$action" == "M " ] ; then
newFileCmd="echo $line | cut -c 3- | sed -e 's|^|$PROJECT_LOCATION/|'"
newFile=$(eval $newFileCmd)
if [ $newlist == "Y" ] ; then
formatFiles="$newFile"
checkIfFormatted=$newFile
newList=N
else
formatFiles="$formatFiles $newFile"
checkIfFormatted="$checkIfFormatted $newFile"
fi
else
newFileCmd="echo $line | cut -c 3- | sed -e 's|^|$PROJECT_LOCATION/|'"
newFile=$(eval $newFileCmd)
echo "Will not check format of file $newFile"
fi
fi
done < $statusFile
rm $statusFile
filteredFileLen=${#formatFiles}
if [ $filteredFileLen -gt 0 ]; then
# Format Java Files
echo "Launching eclipse code formatter… "
eclipseCMD="$ECLIPSE_HOME/eclipse -nosplash -application org.eclipse.jdt.core.JavaCodeFormatter -quiet -config $STYLE_FILE $formatFiles"
formatResults=$(eval $eclipseCMD)
# Check to see if any files have been formatted.
# If files were formatted cancel the commit and notify
# the developer that the fromatted files need to be staged
# and the commit needs to be rerun.
# list files that have been modified
onlyModifiedCmd="git ls-files -m"
modified=$(eval $onlyModifiedCmd)
modifiedLen=${#modified}
#list files that have been staged
onlyStagedCmd="git diff --name-only --cached"
staged=$(eval $onlyStagedCmd)
rc=0
# see if a stage file is also in modified (means the formatter changed the file..)
# tell the developer to do a git add on the file and run the commit again
if [ $modifiedLen -gt 0 ]; then
for f in $checkIfFormatted; do
f=${f#"$PROJECT_LOCATION/"}
for match in $modified; do
if [ "$f" == "$match" ] ; then
echo "File $f has been reformatted .. please do a git add for this file and rerun git commit"
rc=1
fi
done;
done;
fi
else
rc=0
fi
exit $rc