From c5b47d01c1b6f6f8409e9c5e81591c4b64dd2a66 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Fri, 18 Apr 2025 18:45:13 +0800 Subject: [PATCH] Update [ghstack-poisoned] --- README.md | 3 +++ codemcp/config.py | 15 +++++++++++++++ codemcp/file_utils.py | 11 +++++++++++ codemcp/git_commit.py | 26 ++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) diff --git a/README.md b/README.md index 69e1edb1..4c6acd72 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,9 @@ Logs are written to `~/.codemcp/codemcp.log`. The log level can be set in a glob ```toml [logger] verbosity = "INFO" # Can be DEBUG, INFO, WARNING, ERROR, or CRITICAL + +[git] +enabled = true # Set to false to disable all automatic Git operations ``` Logging is not configurable on a per project basis, but this shouldn't matter diff --git a/codemcp/config.py b/codemcp/config.py index e4c87d25..71dd1bb0 100644 --- a/codemcp/config.py +++ b/codemcp/config.py @@ -20,6 +20,7 @@ "get_logger_verbosity", "get_logger_path", "get_line_endings_preference", + "git_operations_enabled", ] # Default configuration values @@ -31,6 +32,9 @@ "files": { "line_endings": None, # Default to OS native or based on configs }, + "git": { + "enabled": True, # By default, git operations are enabled + }, } @@ -136,3 +140,14 @@ def get_line_endings_preference() -> str | None: """ config = load_config() return config["files"]["line_endings"] + + +def git_operations_enabled() -> bool: + """Check if Git operations are enabled in the configuration. + + Returns: + Boolean indicating whether Git operations are enabled. + Default is True if not specified in the config. + """ + config = load_config() + return config["git"]["enabled"] diff --git a/codemcp/file_utils.py b/codemcp/file_utils.py index 11c8a60c..46027ff9 100644 --- a/codemcp/file_utils.py +++ b/codemcp/file_utils.py @@ -8,6 +8,7 @@ from .access import check_edit_permission from .async_file_utils import OpenTextMode +from .config import git_operations_enabled from .git import commit_changes from .line_endings import apply_line_endings, normalize_to_lf @@ -55,6 +56,9 @@ async def check_git_tracking_for_existing_file( ) -> tuple[bool, str | None]: """Check if an existing file is tracked by git. Skips check for non-existent files. + If Git operations are disabled in the configuration, this function will skip + all Git checks and return success. + Args: file_path: The absolute path to the file chat_id: The unique ID to identify the chat session @@ -70,6 +74,13 @@ async def check_git_tracking_for_existing_file( # Normalize the path with tilde expansion file_path = normalize_file_path(file_path) + # If Git operations are disabled, skip all Git checks + if not git_operations_enabled(): + logging.debug( + "Git operations are disabled in config, skipping Git tracking check" + ) + return True, None + # Check if the file exists file_exists = os.path.exists(file_path) diff --git a/codemcp/git_commit.py b/codemcp/git_commit.py index e4b7e196..8e757ff0 100644 --- a/codemcp/git_commit.py +++ b/codemcp/git_commit.py @@ -4,6 +4,7 @@ import os import re +from .config import git_operations_enabled from .git_message import ( update_commit_message_with_description, ) @@ -31,6 +32,9 @@ async def create_commit_reference( in a reference (refs/codemcp/) without changing HEAD. We'll use this to make the "real" commit once our first change happens. + If Git operations are disabled in the configuration, this function will + return a message indicating that Git operations are disabled. + Args: path: The path to the file or directory to commit chat_id: The unique ID of the current chat session @@ -45,6 +49,16 @@ async def create_commit_reference( subprocess.CalledProcessError: If a Git command fails Exception: For other errors during the Git operations """ + # Check if Git operations are enabled + if not git_operations_enabled(): + log.info( + "Git operations are disabled in config, skipping create_commit_reference" + ) + return ( + "Git operations are disabled in configuration, commit reference not created", + "", + ) + if not re.fullmatch(r"^[A-Za-z0-9-]+$", chat_id): raise ValueError(f"Invalid chat_id format: {chat_id}") @@ -173,6 +187,9 @@ async def commit_changes( If commit_all is True, all changes in the repository will be committed. When commit_all is True, path can be None. + If Git operations are disabled in the configuration, this function will + return success but with a message indicating that Git operations are disabled. + Args: path: The path to the file or directory to commit description: Commit message describing the change @@ -190,6 +207,15 @@ async def commit_changes( chat_id, commit_all, ) + + # Check if Git operations are enabled + if not git_operations_enabled(): + log.info("Git operations are disabled in config, skipping commit_changes") + return ( + True, + "Git operations are disabled in configuration, changes not committed", + ) + # First, check if this is a git repository if not await is_git_repository(path): return False, f"Path '{path}' is not in a Git repository"