"""Git commit operations for auto-committing imported notes. Stages new markdown files and stub pages, commits with message format: ingestion: import N notes from [source-type] Supports --no-commit flag. Handles git failures gracefully. """ import logging import subprocess from pathlib import Path logger = logging.getLogger(__name__) def commit_imported_notes(files: list[Path], source_type: str = "import") -> bool: """Stage and commit imported note files. Args: files: List of file paths to stage and commit. source_type: Source description for commit message (e.g., 'batch-import', 'upload', 'nextcloud'). Returns: True if commit succeeded, False otherwise. """ if not files: logger.debug("No files to commit.") return False try: # Stage files file_strs = [str(f) for f in files] subprocess.run( ["git", "add"] + file_strs, capture_output=True, text=True, check=True, ) # Commit count = len(files) message = f"ingestion: import {count} notes from {source_type}" result = subprocess.run( ["git", "commit", "-m", message], capture_output=True, text=True, ) if result.returncode == 0: logger.info("Git commit: %s", message) return True else: # Nothing to commit (files already tracked/unchanged) logger.debug("Git commit returned %d: %s", result.returncode, result.stderr.strip()) return False except FileNotFoundError: logger.warning("Git not found in PATH — skipping commit.") return False except subprocess.CalledProcessError as e: logger.warning("Git operation failed: %s", e.stderr.strip() if e.stderr else str(e)) return False except Exception as e: logger.warning("Unexpected git error: %s", e) return False