Skip to content

Commit 06a79a1

Browse files
committed
Add pre-commit hook to remove cursors.
This patch adds a pre-commit hook specifically for *NIX systems to remove the cursor byte from all files under `src/`. The reason why I chose python is because it is assumed that most *NIX systems already come with it pre-installed. Signed-off-by: xSlendiX <[email protected]>
1 parent e65f27c commit 06a79a1

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

meta/check_code.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
from pathlib import Path
5+
6+
os.chdir(os.path.realpath(os.path.dirname(__file__))+'/../src')
7+
8+
for f in Path(os.getcwd()).glob('**/*'):
9+
if not f.is_file():
10+
continue
11+
12+
if not os.path.basename(f).lower().endswith('.zc'):
13+
continue
14+
15+
contents = b''
16+
with open(f, 'r+b') as file:
17+
contents = file.read()
18+
19+
n = contents.find(b'\x05')
20+
byte = contents[n]
21+
22+
if n == -1: continue
23+
if contents[n-1] == 0 or contents[n-1] > 127: continue
24+
if n+1 < len(contents) and contents[n+1] == 5: continue
25+
if contents[n-1] <= 0x1F and (not contents[n-1] in b'\n\r'): continue
26+
if n+1 < len(contents) and contents[n+1] <= 0x1F and (not contents[n+1] in b'\n\r'): continue
27+
28+
contents = contents[0:n:]
29+
30+
file.seek(0)
31+
file.truncate()
32+
file.write(contents)
33+

meta/install_hooks.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
3+
script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
4+
cd "${script_path}/.." || exit 1
5+
6+
mkdir -p .git/hooks
7+
cp meta/pre-commit .git/hooks/.
8+

meta/pre-commit

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
6+
cd "${script_path}/.." || exit 1
7+
pwd | grep -q '.git' && cd ..
8+
9+
RED='\033[0;31m'
10+
GREEN='\033[0;32m'
11+
NC='\033[0m' # No Color
12+
13+
FAILURES=0
14+
15+
set +e
16+
17+
echo "Running meta/check_code.sh"
18+
if time meta/check_code.sh "$@" && git diff --exit-code; then
19+
echo -e "[${GREEN}GOOD${NC}]: meta/check_code.sh"
20+
else
21+
echo -e "[${RED}FAIL${NC}]: meta/check_code.sh"
22+
((FAILURES+=1))
23+
fi
24+
25+
exit "${FAILURES}"

0 commit comments

Comments
 (0)