Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/forge/workspace/git_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ def pull_rebase(self, remote: str = "fork") -> None:
branch = self.workspace.branch_name
logger.info(f"Syncing with {remote}/{branch} before implementing changes")
self._run_git("fetch", remote)
if not self.remote_branch_exists(branch, remote=remote):
logger.info(
"Remote branch %s/%s does not exist yet; skipping rebase before first push",
remote,
branch,
)
return
self._run_git("rebase", f"{remote}/{branch}")
logger.info(f"Rebase onto {remote}/{branch} complete")

Expand Down
49 changes: 49 additions & 0 deletions tests/unit/workspace/test_git_ops_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Tests for synchronizing implementation workspaces with remote branches."""

from pathlib import Path
from unittest.mock import MagicMock, call, patch

from forge.workspace.git_ops import GitOperations
from forge.workspace.manager import Workspace


def _git_ops(tmp_path: Path) -> GitOperations:
workspace = Workspace(
path=tmp_path / "repo",
repo_name="org/repo",
branch_name="forge/test-123",
ticket_key="TEST-123",
)
with patch("forge.workspace.git_ops.get_settings", return_value=MagicMock()):
return GitOperations(workspace)


def test_pull_rebase_skips_missing_remote_branch_before_first_push(tmp_path):
"""A new implementation branch is local-only until its first backup push."""
git = _git_ops(tmp_path)

with (
patch.object(git, "_run_git") as run_git,
patch.object(git, "remote_branch_exists", return_value=False) as branch_exists,
):
git.pull_rebase(remote="origin")

run_git.assert_called_once_with("fetch", "origin")
branch_exists.assert_called_once_with("forge/test-123", remote="origin")


def test_pull_rebase_rebases_when_remote_branch_exists(tmp_path):
"""Existing backup branches are still incorporated before implementation."""
git = _git_ops(tmp_path)

with (
patch.object(git, "_run_git") as run_git,
patch.object(git, "remote_branch_exists", return_value=True) as branch_exists,
):
git.pull_rebase(remote="fork")

assert run_git.call_args_list == [
call("fetch", "fork"),
call("rebase", "fork/forge/test-123"),
]
branch_exists.assert_called_once_with("forge/test-123", remote="fork")
Loading