Coverage for run_ruff.py: 0%

48 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-21 15:08 +0000

1#!/usr/bin/env python3 

2 

3import shutil 

4import subprocess 

5import sys 

6from pathlib import Path 

7from tempfile import NamedTemporaryFile 

8 

9 

10def get_staged_python_files(): 

11 result = subprocess.run( 

12 ['git', 'diff', '--cached', '--name-only', '--diff-filter=ACM'], capture_output=True, text=True 

13 ) 

14 return [f for f in result.stdout.strip().splitlines() if f.endswith('.py')] 

15 

16 

17def get_changed_lines(file_path): 

18 result = subprocess.run(['git', 'diff', '--cached', '-U0', file_path], capture_output=True, text=True) 

19 lines = [] 

20 for line in result.stdout.splitlines(): 

21 if line.startswith('@@'): 

22 parts = line.split(' ') 

23 added = parts[2] # e.g., '+10,2' or '+5' 

24 start, _, count = added[1:].partition(',') 

25 start = int(start) 

26 count = int(count) if count else 1 

27 lines.extend(range(start, start + count)) 

28 return set(lines) 

29 

30 

31def apply_fixes_to_changed_lines(file_path, changed_lines): 

32 # Create a backup 

33 original = Path(file_path).read_text() 

34 

35 # Let Ruff fix the whole file into a temp file 

36 with NamedTemporaryFile('w+', delete=False) as temp: 

37 temp_path = temp.name 

38 shutil.copyfile(file_path, temp_path) 

39 subprocess.run(['ruff', 'check', '--fix', '--select', 'Q', temp_path], capture_output=True) 

40 

41 original_lines = original.splitlines() 

42 fixed_lines = Path(temp_path).read_text().splitlines() 

43 

44 # Apply only the fixed lines that are within changed lines 

45 new_lines = [] 

46 for i, (orig, fixed) in enumerate(zip(original_lines, fixed_lines), start=1): 

47 if i in changed_lines and orig != fixed: 

48 new_lines.append(fixed) 

49 else: 

50 new_lines.append(orig) 

51 

52 Path(file_path).write_text('\n'.join(new_lines) + '\n') 

53 subprocess.run(['git', 'add', file_path]) 

54 

55 

56def main(): 

57 staged_files = get_staged_python_files() 

58 

59 if not staged_files: 

60 sys.exit(0) 

61 

62 for file in staged_files: 

63 changed_lines = get_changed_lines(file) 

64 if not changed_lines: 

65 continue 

66 

67 apply_fixes_to_changed_lines(file, changed_lines) 

68 

69 print('✅ Ruff autofix applied to changed lines only.') 

70 sys.exit(0) 

71 

72 

73if __name__ == '__main__': 

74 main()