From 10385bb1c2285fa89e29ce4baab05a4e11fcd151 Mon Sep 17 00:00:00 2001 From: bartool Date: Fri, 6 Mar 2026 20:40:48 +0100 Subject: [PATCH] feat: implement .gitignore support in file watcher --- git_monitor/file_watcher.py | 7 ++++++- git_monitor/git_manager.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/git_monitor/file_watcher.py b/git_monitor/file_watcher.py index 037e387..f3c3281 100644 --- a/git_monitor/file_watcher.py +++ b/git_monitor/file_watcher.py @@ -47,10 +47,15 @@ class GitEventHandler(FileSystemEventHandler): file_path = event.src_path if action != "rename" else event.dest_path file_name = os.path.basename(file_path) - # Check if any part of the path is .git + # Check if any part of the path is .git or if it's the log file if ".git" in file_path.split(os.sep) or file_name == "git_monitor.log": return + # NEW: Respect .gitignore + if git_manager.is_ignored(file_path): + logger.info(f"Ignored: {file_path} (matches .gitignore)") + return + display_name = custom_file_name if custom_file_name else file_name logger.info(f"File event: {action} on {display_name}") diff --git a/git_monitor/git_manager.py b/git_monitor/git_manager.py index 110ac12..c79e7b4 100644 --- a/git_monitor/git_manager.py +++ b/git_monitor/git_manager.py @@ -34,6 +34,18 @@ class GitManager: 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.")