73 lines
2.6 KiB
Python
73 lines
2.6 KiB
Python
import os
|
|
from git import Repo, exc
|
|
from git_monitor.logger import logger
|
|
from git_monitor.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 is_ignored(self, file_path):
|
|
"""Check if a file is ignored by .gitignore rules."""
|
|
if not self.repo:
|
|
return False
|
|
try:
|
|
# git check-ignore returns the path if it is ignored
|
|
ignored_files = self.repo.ignored(file_path)
|
|
return len(ignored_files) > 0
|
|
except Exception as e:
|
|
logger.error(f"Error checking ignore status for {file_path}: {e}")
|
|
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()
|