refactor: move files to git_monitor/ directory and add run.py

This commit is contained in:
2026-03-06 19:30:27 +01:00
parent d2556bf656
commit 1b6c7dcc01
9 changed files with 13 additions and 0 deletions

View 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()