Skip to content

Commit 5281303

Browse files
committed
pre-commit added
1 parent 3654296 commit 5281303

3 files changed

Lines changed: 107 additions & 0 deletions

File tree

.pre-commit-config.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
fail_fast: true
4+
default_install_hook_types:
5+
- pre-commit
6+
- pre-push
7+
repos:
8+
- repo: local
9+
hooks:
10+
11+
# - id: ruff-auto-fix
12+
# name: ruff-auto-fix
13+
# entry: ./run_ruff.py
14+
# language: python
15+
# types: [ python ]
16+
# always_run: true
17+
# pass_filenames: false
18+
19+
# - id: pytest-check
20+
# name: pytest-check
21+
# entry: make test
22+
# stages:
23+
# - pre-push
24+
# language: system
25+
# pass_filenames: false
26+
# always_run: true
27+
28+
- id: conventional-commits-check
29+
name: Conventional Commits Check
30+
entry: ./conventional_commits_check.py
31+
language: python
32+
stages: [commit-msg]

requirements.txt

24 Bytes
Binary file not shown.

run_ruff.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python3
2+
3+
import json
4+
import shutil
5+
import subprocess
6+
import sys
7+
from pathlib import Path
8+
from tempfile import NamedTemporaryFile
9+
10+
11+
def get_staged_python_files():
12+
result = subprocess.run(
13+
['git', 'diff', '--cached', '--name-only', '--diff-filter=ACM'], capture_output=True, text=True
14+
)
15+
return [f for f in result.stdout.strip().splitlines() if f.endswith('.py')]
16+
17+
18+
def get_changed_lines(file_path):
19+
result = subprocess.run(['git', 'diff', '--cached', '-U0', file_path], capture_output=True, text=True)
20+
lines = []
21+
for line in result.stdout.splitlines():
22+
if line.startswith('@@'):
23+
parts = line.split(' ')
24+
added = parts[2] # e.g., '+10,2' or '+5'
25+
start, _, count = added[1:].partition(',')
26+
start = int(start)
27+
count = int(count) if count else 1
28+
lines.extend(range(start, start + count))
29+
return set(lines)
30+
31+
32+
def apply_fixes_to_changed_lines(file_path, changed_lines):
33+
# Create a backup
34+
original = Path(file_path).read_text()
35+
36+
# Let Ruff fix the whole file into a temp file
37+
with NamedTemporaryFile('w+', delete=False) as temp:
38+
temp_path = temp.name
39+
shutil.copyfile(file_path, temp_path)
40+
subprocess.run(['ruff', 'check', '--fix', '--select', 'Q', temp_path], capture_output=True)
41+
42+
original_lines = original.splitlines()
43+
fixed_lines = Path(temp_path).read_text().splitlines()
44+
45+
# Apply only the fixed lines that are within changed lines
46+
new_lines = []
47+
for i, (orig, fixed) in enumerate(zip(original_lines, fixed_lines), start=1):
48+
if i in changed_lines and orig != fixed:
49+
new_lines.append(fixed)
50+
else:
51+
new_lines.append(orig)
52+
53+
Path(file_path).write_text('\n'.join(new_lines) + '\n')
54+
subprocess.run(['git', 'add', file_path])
55+
56+
57+
def main():
58+
staged_files = get_staged_python_files()
59+
60+
if not staged_files:
61+
sys.exit(0)
62+
63+
for file in staged_files:
64+
changed_lines = get_changed_lines(file)
65+
if not changed_lines:
66+
continue
67+
68+
apply_fixes_to_changed_lines(file, changed_lines)
69+
70+
print('✅ Ruff autofix applied to changed lines only.')
71+
sys.exit(0)
72+
73+
74+
if __name__ == '__main__':
75+
main()

0 commit comments

Comments
 (0)