-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_manager.py
55 lines (48 loc) · 2.08 KB
/
git_manager.py
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
# git_manager.py
import subprocess
import logging
from typing import Optional
from exceptions import GitError
class GitCommitManager:
"""Manage Git operations"""
def __init__(self, logger: logging.Logger):
self.logger = logger
def check_prerequisites(self) -> bool:
"""Check Git installation and repository status"""
try:
self._run_git_command(["--version"])
self._run_git_command(["rev-parse", "--is-inside-work-tree"])
self.logger.debug("Git prerequisites check passed")
return True
except subprocess.CalledProcessError:
self.logger.critical("Current directory is not a Git repository")
return False
except FileNotFoundError:
self.logger.critical("Git is not installed on the system")
return False
def get_diff(self) -> Optional[str]:
"""Get staged changes from Git"""
try:
result = self._run_git_command(["diff", "--cached"], capture_output=True)
diff_output = result.stdout.decode('utf-8').strip() if result.stdout else None
if diff_output:
self.logger.debug("Successfully retrieved git diff")
else:
self.logger.warning("No staged changes found")
return diff_output
except subprocess.CalledProcessError as e:
self.logger.error(f"Failed to get git diff: {str(e)}")
return None
def commit(self, message: str) -> bool:
"""Commit changes with the given message"""
try:
self._run_git_command(["commit", "-m", str(message)])
self.logger.info("Changes committed successfully")
return True
except subprocess.CalledProcessError as e:
self.logger.error(f"Failed to commit changes: {str(e)}")
return False
@staticmethod
def _run_git_command(args: list, **kwargs) -> subprocess.CompletedProcess:
"""Run a Git command with given arguments"""
return subprocess.run(["git"] + args, **kwargs, check=True)