-
Notifications
You must be signed in to change notification settings - Fork 7
/
githook-pre-commit
executable file
·59 lines (52 loc) · 1.52 KB
/
githook-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
#!/usr/bin/env bash
### Create pre-commit symlink if unset ###
GITDIR="";
if [ -d .git ]; then
GITDIR=".git";
elif [ -f .git ]; then
GITDIR=$(sed -n '/^gitdir:/{ s|.*: ||; p; }' .git);
fi
if [ ! -d "$GITDIR" ]; then
echo "${0##*/}: error: unable to find git directory" 1>&2;
exit 1;
fi
if [ ! -h "$GITDIR/hooks/pre-commit" ]; then
if [ $(realpath --help 2>&1 | grep -c relative) != 0 ]; then
HOOK=$(realpath --relative-to="$GITDIR/hooks" ./githook-pre-commit);
else
HOOK=$(readlink -f ./githook-pre-commit);
fi
ln -fs "$HOOK" "$GITDIR/hooks/pre-commit";
echo "${0##*/}: creating git pre-commit hook symlink" 1>&2;
exit 1;
fi
### Update versions on files ###
FILES=( $(git status --porcelain | sed -r 's|^ |_|; s|^(.) |\1_|;' | grep -E '^([MRA]|.M)') );
V=$(date -u +%Y.%m.%d);
check_change_after_staged () {
[ "${2:1:1}" = "M" ] &&
echo "${0##*/}: error: aborting due to file change after staged: $1" 1>&2 &&
exit 1;
}
update_file_version () {
echo "${0##*/}: updating version of $1" 1>&2;
sed -r -i 's|([$"])Version:[^$"]*([$"])|\1Version: '"$V"'\2|' "$1";
git add "$1";
}
n=1;
while [ "$n" -lt "${#FILES[@]}" ]; do
check_change_after_staged "${FILES[$n]}" "${FILES[$((n-1))]}";
case "${FILES[$n]}" in
tesseract-recognize.cc )
update_file_version "${FILES[$n]}";
;;
*.py )
update_file_version "${FILES[$n]}";
echo "${0##*/}: pylint ${FILES[$n]}" 1>&2;
pylint --errors-only "${FILES[$n]}";
;;
esac
[ "$?" != "0" ] && exit 1;
n=$((n+2));
done
exit 0;