Skip to content
Closed
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
3 changes: 3 additions & 0 deletions src/forge/workspace/git_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ 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(f"Remote branch {remote}/{branch} does not exist yet; skipping rebase")
return
self._run_git("rebase", f"{remote}/{branch}")
logger.info(f"Rebase onto {remote}/{branch} complete")

Expand Down
47 changes: 47 additions & 0 deletions tests/unit/workspace/test_pull_rebase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Tests for GitOperations.pull_rebase behavior."""

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

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


def _git_ops(tmp_path: Path) -> GitOperations:
settings = MagicMock()
settings.github_token.get_secret_value.return_value = "ghp_fake"
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=settings):
return GitOperations(workspace)


def test_pull_rebase_skips_when_remote_branch_missing(tmp_path):
"""First implementation run: remote branch doesn't exist yet, rebase must be skipped."""
git = _git_ops(tmp_path)

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

run_git.assert_called_once_with("fetch", "origin")


def test_pull_rebase_rebases_when_remote_branch_exists(tmp_path):
"""Subsequent runs: remote branch exists, rebase must happen."""
git = _git_ops(tmp_path)

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

assert run_git.call_args_list[0].args == ("fetch", "origin")
assert run_git.call_args_list[1].args == ("rebase", "origin/forge/test-123")
Loading