From 9a291e385b72d53da4af45d2a97e0d4ffd862e40 Mon Sep 17 00:00:00 2001 From: Tim O'Farrell Date: Mon, 14 Jul 2025 10:17:26 -0600 Subject: [PATCH] Introduced config field to determine whether to init a git repo (#9693) --- openhands/core/config/openhands_config.py | 1 + openhands/runtime/base.py | 7 +++--- tests/unit/test_runtime_git_tokens.py | 29 +++-------------------- 3 files changed, 7 insertions(+), 30 deletions(-) diff --git a/openhands/core/config/openhands_config.py b/openhands/core/config/openhands_config.py index eb621cf0f6..16f931400a 100644 --- a/openhands/core/config/openhands_config.py +++ b/openhands/core/config/openhands_config.py @@ -89,6 +89,7 @@ class OpenHandsConfig(BaseModel): run_as_openhands: bool = Field(default=True) max_iterations: int = Field(default=OH_MAX_ITERATIONS) max_budget_per_task: float | None = Field(default=None) + init_git_in_empty_workspace: bool = Field(default=False) disable_color: bool = Field(default=False) jwt_secret: SecretStr | None = Field(default=None) diff --git a/openhands/runtime/base.py b/openhands/runtime/base.py index 854f3fc823..1729160e4c 100644 --- a/openhands/runtime/base.py +++ b/openhands/runtime/base.py @@ -367,9 +367,7 @@ class Runtime(FileEditRuntimeMixin): selected_branch: str | None, ) -> str: if not selected_repository: - # In SaaS mode (indicated by user_id being set), always run git init - # In OSS mode, only run git init if workspace_base is not set - if self.user_id or not self.config.workspace_base: + if self.config.init_git_in_empty_workspace: logger.debug( 'No repository selected. Initializing a new git repository in the workspace.' ) @@ -1062,7 +1060,8 @@ fi def get_git_changes(self, cwd: str) -> list[dict[str, str]] | None: self.git_handler.set_cwd(cwd) - return self.git_handler.get_git_changes() + changes = self.git_handler.get_git_changes() + return changes def get_git_diff(self, file_path: str, cwd: str) -> dict[str, str]: self.git_handler.set_cwd(cwd) diff --git a/tests/unit/test_runtime_git_tokens.py b/tests/unit/test_runtime_git_tokens.py index da68c4e29c..55ed5b5816 100644 --- a/tests/unit/test_runtime_git_tokens.py +++ b/tests/unit/test_runtime_git_tokens.py @@ -219,33 +219,10 @@ async def test_export_latest_git_provider_tokens_token_update(runtime): @pytest.mark.asyncio -async def test_clone_or_init_repo_no_repo_with_user_id(temp_dir): - """Test that git init is run when no repository is selected and user_id is set""" +async def test_clone_or_init_repo_no_repo_init_git_in_empty_workspace(temp_dir): + """Test that git init is run when no repository is selected and init_git_in_empty_workspace""" config = OpenHandsConfig() - file_store = get_file_store('local', temp_dir) - event_stream = EventStream('abc', file_store) - runtime = TestRuntime( - config=config, event_stream=event_stream, sid='test', user_id='test_user' - ) - - # Call the function with no repository - result = await runtime.clone_or_init_repo(None, None, None) - - # Verify that git init was called - assert len(runtime.run_action_calls) == 1 - assert isinstance(runtime.run_action_calls[0], CmdRunAction) - assert ( - runtime.run_action_calls[0].command - == f'git init && git config --global --add safe.directory {runtime.workspace_root}' - ) - assert result == '' - - -@pytest.mark.asyncio -async def test_clone_or_init_repo_no_repo_no_user_id_no_workspace_base(temp_dir): - """Test that git init is run when no repository is selected, no user_id, and no workspace_base""" - config = OpenHandsConfig() - config.workspace_base = None # Ensure workspace_base is not set + config.init_git_in_empty_workspace = True file_store = get_file_store('local', temp_dir) event_stream = EventStream('abc', file_store) runtime = TestRuntime(