Introduced config field to determine whether to init a git repo (#9693)

This commit is contained in:
Tim O'Farrell 2025-07-14 10:17:26 -06:00 committed by GitHub
parent 95ccec82d9
commit 9a291e385b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 7 additions and 30 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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(