-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_helper.py
More file actions
73 lines (66 loc) · 2.32 KB
/
git_helper.py
File metadata and controls
73 lines (66 loc) · 2.32 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
"""
GitHelper - Helper class for Git operations
"""
import subprocess
import sys
from typing import List, Tuple, Optional
class GitHelper:
"""Helper class for Git operations"""
@staticmethod
def has_untracked_files() -> Tuple[bool, List[str]]:
"""Check for untracked files in the repository"""
try:
result = subprocess.run(
['git', 'ls-files', '--others', '--exclude-standard'],
capture_output=True,
text=True,
check=True
)
files = [f.strip() for f in result.stdout.split('\n') if f.strip()]
return len(files) > 0, files
except subprocess.CalledProcessError:
return False, []
@staticmethod
def get_staged_files() -> List[str]:
"""Get list of staged files"""
try:
result = subprocess.run(
['git', 'diff', '--staged', '--name-only'],
capture_output=True,
text=True,
check=True
)
return [f.strip() for f in result.stdout.split('\n') if f.strip()]
except subprocess.CalledProcessError:
return []
@staticmethod
def get_file_diff(filepath: str) -> Optional[str]:
"""Get diff for specific file"""
try:
result = subprocess.run(
['git', 'diff', '--staged', '--', filepath],
capture_output=True,
text=True,
check=True
)
return result.stdout if result.stdout.strip() else None
except subprocess.CalledProcessError as e:
from rich.console import Console
console = Console()
console.print(f"[danger]Error getting diff for {filepath}: {e}[/danger]")
return None
@staticmethod
def commit_changes(message: str) -> bool:
"""Commit changes with the given message"""
try:
subprocess.run(
['git', 'commit', '-m', message],
check=True
)
return True
except subprocess.CalledProcessError as e:
from rich.console import Console
console = Console()
console.print(f"[danger]Error committing changes: {e}[/danger]")
return False