-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpre-commit
executable file
·54 lines (45 loc) · 1.59 KB
/
pre-commit
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
#!/bin/sh
####################################################################################
# download formatter
####################################################################################
mkdir -p .cache
cd .cache
formatter_name="google-java-format-1.7-all-deps.jar"
formatter_url="https://github.com/google/google-java-format/releases/download/google-java-format-1.7/"$formatter_name
if [ ! -f $formatter_name ]
then
if [ -z `hash wget` ]
then
wget $formatter_url
else
curl -O $formatter_url
fi
chmod 755 $formatter_name
fi
cd ..
####################################################################################
# get staged files
####################################################################################
changed_java_files=$(git diff --cached --name-only --diff-filter=ACMR | grep ".*java$" )
echo $changed_java_files
#if [ ! -z "$changed_java_files" ]
#then
# java -jar .cache/$formatter_name -replace $changed_java_files
#fi
####################################################################################
# Format staged files
####################################################################################
for file in $changed_java_files
do
echo "Formatting $file"
# Get the file from index
git show ":$file" > "$file.tmp.java"
# Format it
java -jar .cache/$formatter_name -replace "$file.tmp.java"
# Create a blob object from the formatted file
hash=`git hash-object -w "$file.tmp.java"`
# Add it back to index
git update-index --add --cacheinfo 100644 "$hash" "$file"
# Remove the tmp file
rm "$file.tmp.java"
done