Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Initialize git repo in workspace when no GitHub repo is selected (#7904)

Robert Brennanbf9f2aa7a5ef
diff --git a/openhands/core/setup.py b/openhands/core/setup.py
index c403ae737f..219f9dd9f3 100644
--- a/openhands/core/setup.py
+++ b/openhands/core/setup.py
@@ -115,18 +115,16 @@ def initialize_repository_for_runtime(
)
provider_tokens = secret_store.provider_tokens if secret_store else None
- repo_directory = None
- if selected_repository and provider_tokens:
- logger.debug(f'Selected repository {selected_repository}.')
- repo_directory = call_async_from_sync(
- runtime.clone_repo,
- GENERAL_TIMEOUT,
- provider_tokens,
- selected_repository,
- None,
- )
- # Run setup script if it exists
- runtime.maybe_run_setup_script()
+ logger.debug(f'Selected repository {selected_repository}.')
+ repo_directory = call_async_from_sync(
+ runtime.clone_or_init_repo,
+ GENERAL_TIMEOUT,
+ provider_tokens,
+ selected_repository,
+ None,
+ )
+ # Run setup script if it exists
+ runtime.maybe_run_setup_script()
return repo_directory
diff --git a/openhands/runtime/base.py b/openhands/runtime/base.py
index 7ae5315a41..1ab43bf012 100644
--- a/openhands/runtime/base.py
+++ b/openhands/runtime/base.py
@@ -309,13 +309,28 @@ class Runtime(FileEditRuntimeMixin):
return
self.event_stream.add_event(observation, source) # type: ignore[arg-type]
- async def clone_repo(
+ async def clone_or_init_repo(
self,
- git_provider_tokens: PROVIDER_TOKEN_TYPE,
- selected_repository: str | Repository,
+ git_provider_tokens: PROVIDER_TOKEN_TYPE | None,
+ selected_repository: str | Repository | None,
selected_branch: str | None,
repository_provider: ProviderType = ProviderType.GITHUB,
) -> str:
+ if not selected_repository:
+ if self.config.workspace_base:
+ logger.info(
+ 'In workspace mount mode, not initializing a new git repository.'
+ )
+ return ''
+ logger.debug(
+ 'No repository selected. Initializing a new git repository in the workspace.'
+ )
+ action = CmdRunAction(
+ command='git init',
+ )
+ self.run_action(action)
+ return ''
+
provider_domains = {
ProviderType.GITHUB: 'github.com',
ProviderType.GITLAB: 'gitlab.com',
@@ -327,9 +342,11 @@ class Runtime(FileEditRuntimeMixin):
else selected_repository.git_provider
)
+ if not git_provider_tokens:
+ raise RuntimeError('Need git provider tokens to clone repo')
git_token = git_provider_tokens[chosen_provider].token
if not git_token:
- raise RuntimeError('Require valid git token to clone repo')
+ raise RuntimeError('Need a valid git token to clone repo')
domain = provider_domains[chosen_provider]
repository = (
diff --git a/openhands/server/session/agent_session.py b/openhands/server/session/agent_session.py
index 0178675719..2e2596e935 100644
--- a/openhands/server/session/agent_session.py
+++ b/openhands/server/session/agent_session.py
@@ -323,11 +323,10 @@ class AgentSession:
)
return False
- if selected_repository and git_provider_tokens:
- await self.runtime.clone_repo(
- git_provider_tokens, selected_repository, selected_branch
- )
- await call_sync_from_async(self.runtime.maybe_run_setup_script)
+ await self.runtime.clone_or_init_repo(
+ git_provider_tokens, selected_repository, selected_branch
+ )
+ await call_sync_from_async(self.runtime.maybe_run_setup_script)
self.logger.debug(
f'Runtime initialized with plugins: {[plugin.name for plugin in self.runtime.plugins]}'