forked from duckdb/duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_git_hooks.py
More file actions
55 lines (45 loc) · 1.67 KB
/
Copy pathadd_git_hooks.py
File metadata and controls
55 lines (45 loc) · 1.67 KB
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
#!/usr/bin/env python3
"""Adds `make format-fix` functionality to .git/hooks/pre-commit."""
import os
HOOK_MARKER = '# make format-fix hook #'
HOOK_PATH = '.git/hooks/pre-commit'
FORMAT_SCRIPT = (
'duckdb/scripts/format.py' if os.path.isfile('duckdb/scripts/format.py') else 'scripts/format.py'
) # this is to support extension repos
FORMAT_CMD = f'python3 {FORMAT_SCRIPT} {{}} --fix --noconfirm'
HOOK_BODY = f"""\
# make format-fix hook #
# Get list of staged files
staged_files=$(git diff --cached --name-only)
# Run formatter on staged files
echo "$staged_files" | xargs -P 0 -I {{}} {FORMAT_CMD}
# Re-stage files that were:
# 1. Originally staged
# 2. Modified by the formatter
echo "$staged_files" | while read -r file; do
if [ -n "$file" ] && git diff --name-only | grep -Fxq "$file"; then
git add "$file"
fi
done
"""
def main():
if os.path.isfile(HOOK_PATH):
with open(HOOK_PATH, 'r') as f:
content = f.read()
if HOOK_MARKER in content:
print("Hook already exists, no action needed. exiting.")
else:
print(
".git/hooks/pre-commit file exists but does not contain make `make format-fix` functionality for staged files"
)
print("Adding `make format-fix` functionality for staged files")
with open(HOOK_PATH, 'a') as f:
f.write(HOOK_BODY)
else:
print("Creating .git/hooks/pre-commit file with `make format-fix` functionality for staged files")
with open(HOOK_PATH, 'w') as f:
f.write('#!/bin/sh\n' + HOOK_BODY)
os.chmod(HOOK_PATH, 0o755)
print("Done! ✅")
if __name__ == '__main__':
main()