-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpre-commit
executable file
·116 lines (103 loc) · 3.21 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
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
#!/bin/sh
# pre-commit hook for Sketch files Git tracking
# Set UTF-8 encoding
export LANG=UTF-8
# Needed to work in SourceTree
export PATH=/usr/local/bin:$PATH
# Git repository absolute path
git_root=`git rev-parse --show-toplevel`
# Unzipped Sketch files path
unzipped_root="$git_root/_unzipped"
# Untouched Sketch files path
untouched_root="$git_root/_untouched"
# Array of all staged files
git_diff=`git diff --name-only --cached | grep ".sketch$"`
# Check if any Sketch file is staged
if [[ ${git_diff[0]} == '' ]]
then
echo "No Sketch file staged."
else
while read staged_sketch
do
# Absolute Sketch file path
sketch="$git_root/$staged_sketch"
# Unzipped Sketch file directory path
unzipped_directory="$unzipped_root/$staged_sketch"
# Untouched Sketch file path
untouched_sketch="$untouched_root/$staged_sketch"
# Untouched Sketch file directory path
untouched_directory=`dirname "$untouched_sketch"`
echo "Processing '`basename $sketch`'."
# Delete existing directory
rm -rf "$unzipped_directory"
# If Sketch file wasn't deleted
if ! git diff --summary --cached -- $staged_sketch | grep "delete" 2>&1 > /dev/null
then
# Copy untouched Sketch file to untouched folder
if mkdir -p "$untouched_directory"
then
if cp "$sketch" "$untouched_sketch"
then
echo " Untouched '`basename $sketch`' backed up."
else
echo " Couldn't copy untouched '`basename $sketch`'."
exit 1
fi
else
echo " Couldn't create '$untouched_root' directory."
exit 1
fi
# Stage untouched file
git add "$untouched_sketch"
# Create directory
if mkdir -p "$unzipped_directory"
then
if cd "$unzipped_directory"
then
# Unzip Sketch file
if unzip -q "$sketch"
then
echo " '`basename $sketch`' unzipped."
# Prettify all JSON files
for json_file in $(find . -name "*.json")
do
if python3 -m json.tool "$json_file" "$json_file".pretty
then
if mv "$json_file".pretty "$json_file"
then
echo " '$json_file' prettified."
else
echo " Couldn't move prettified '$json_file'."
rm -rf "$unzipped_directory"
exit 1
fi
else
echo " Couldn't prettify '$json_file'."
rm -rf "$unzipped_directory"
exit 1
fi
done
else
echo " Couldn't unzip '`basename $sketch`'."
rm -rf "$unzipped_directory"
exit 1
fi
cd "$git_root"
else
echo " Couldn't change directory to '$unzipped_directory'."
exit 1
fi
else
echo " Couldn't create directory '$unzipped_directory'."
exit 1
fi
else
rm "$sketch"
rm -rf "$untouched_sketch"
echo " '`basename $sketch`' was removed with all unzipped and untouched data."
fi
git add "$unzipped_directory/*"
# Unstage working Sketch file
git rm --cached "$sketch"
done <<< "$git_diff"
fi