-
Notifications
You must be signed in to change notification settings - Fork 13
/
pre-commit
executable file
·71 lines (59 loc) · 1.92 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
#!/bin/bash
#
# pre-commit-excel: have git handle xlsx files by deflating them and storing
# the XML in the repository, not the zip file.
#
# companion to post-commit-excel
#
# -- config -- #
TMP_PREFIX='.~' # prefix XML directory--should NOT conflict with
# anything in your repo
# -- end config -- #
echo
echo "=== Pre-commit XLSX processing ===="
unpack_xlsx () {
# unzip the xlsx file
if [ -e "$1" ]; then
if [ -e "${TMP_PREFIX}$1" ]; then rm -rf "${TMP_PREFIX}$1"; fi
unzip "$1" -d "${TMP_PREFIX}$1"
else
# abort the commit if this isn't an excel file somehow
echo "$1 is not an xlsx file. this is a bug."
exit 3
fi
}
# create a placeholder text file for the .xlsx file, which uniquely identifies
# it and prevents losing work on a checkout
replace_with_placeholder () {
sha1sum "$1" | tee "$1" > /dev/null # hack for overwriting without temp file
}
reformat_xmls () {
# reformat the xml file to many lines
find "$1" -name "*.xml" -type f -exec xmllint --output '{}' --format '{}' \;
}
# check that the swap file isn't there (if the xlsx has been staged)
for i in $(git diff --name-only --cached -- *.xlsx); do
dir=$(dirname "$i")
file=$(basename "$i")
if [ -e "$dir/~\$$file" ]; then
echo "$file swap file present: close xlsx file before committing"
exit 2 # excel file is open
fi
done
# unpack all the zipfiles (if the xlsx has been staged)
for i in $(git diff --name-only --cached -- *.xlsx); do
dir=$(dirname "$i")
file=$(basename "$i")
cd "$dir"
unpack_xlsx "$file"
replace_with_placeholder "$file"
echo -n "Commit placeholder file: "
file "$file"
reformat_xmls "${TMP_PREFIX}$file"
cd -
# stage the XML files and the placeholder
git add "$dir/${TMP_PREFIX}$file"
git add "$dir/$file"
done
echo "=== End of pre-commit processing ==="
echo