feat: add git_manager.py for Git operations
This commit is contained in:
60
git_manager.py
Normal file
60
git_manager.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import os
|
||||
from git import Repo, exc
|
||||
from logger import logger
|
||||
from notifier import notifier
|
||||
|
||||
class GitManager:
|
||||
def __init__(self, repo_path=None):
|
||||
self.repo_path = repo_path
|
||||
self.repo = None
|
||||
if repo_path:
|
||||
self.load_repository(repo_path)
|
||||
|
||||
def load_repository(self, path):
|
||||
if self.is_git_repository(path):
|
||||
try:
|
||||
self.repo = Repo(path)
|
||||
self.repo_path = path
|
||||
logger.info(f"Loaded repository: {path}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to load repository {path}: {e}")
|
||||
notifier.notify("Git Error", f"Failed to load repository: {e}")
|
||||
else:
|
||||
logger.warning(f"{path} is not a valid Git repository.")
|
||||
notifier.notify("Git Error", f"{path} is not a valid Git repository.")
|
||||
return False
|
||||
|
||||
def is_git_repository(self, path):
|
||||
if not path or not os.path.isdir(path):
|
||||
return False
|
||||
try:
|
||||
Repo(path).git_dir
|
||||
return True
|
||||
except (exc.InvalidGitRepositoryError, exc.NoSuchPathError):
|
||||
return False
|
||||
|
||||
def commit_change(self, action, file_name):
|
||||
if not self.repo:
|
||||
logger.error("No repository loaded for commit.")
|
||||
return False
|
||||
|
||||
commit_msg = f"{action}: {file_name}"
|
||||
try:
|
||||
# Stage all changes (simple approach for the CNC operator use-case)
|
||||
self.repo.git.add(A=True)
|
||||
# Check if there are changes to commit
|
||||
if self.repo.is_dirty(untracked_files=True):
|
||||
self.repo.index.commit(commit_msg)
|
||||
logger.info(f"Committed: {commit_msg}")
|
||||
notifier.notify("Git Monitor", commit_msg + " committed")
|
||||
return True
|
||||
else:
|
||||
logger.info(f"No changes to commit for: {file_name}")
|
||||
return False
|
||||
except Exception as e:
|
||||
logger.error(f"Git commit failed: {e}")
|
||||
notifier.notify("Git Error", f"Commit failed: {e}")
|
||||
return False
|
||||
|
||||
git_manager = GitManager()
|
||||
Reference in New Issue
Block a user