Skip to content

Commit 697a245

Browse files
committed
git_utils: Fix '--force' option
1 parent 4e45743 commit 697a245

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/mbed_tools/project/_internal/git_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def checkout(repo: git.Repo, ref: str, force: bool = False) -> None:
5555
VersionControlError: Check out failed.
5656
"""
5757
try:
58-
repo.git.checkout(f"--force {ref}" if force else str(ref))
58+
git_args = [ref] + ["--force"] if force else [ref]
59+
repo.git.checkout(*git_args)
5960
except git.exc.GitCommandError as err:
6061
raise VersionControlError(f"Failed to check out revision '{ref}'. Error from VCS: {err}")
6162

tests/project/_internal/test_git_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,14 @@ def test_raises_version_control_error_when_no_git_repo_found(self, mock_repo):
8484

8585
class TestCheckout:
8686
def test_git_lib_called_with_correct_command(self, mock_repo):
87+
git_utils.checkout(mock_repo, "master")
88+
89+
mock_repo.git.checkout.assert_called_once_with("master")
90+
91+
def test_git_lib_called_with_correct_command_with_force(self, mock_repo):
8792
git_utils.checkout(mock_repo, "master", force=True)
8893

89-
mock_repo.git.checkout.assert_called_once_with("--force master")
94+
mock_repo.git.checkout.assert_called_once_with("master", "--force")
9095

9196
def test_raises_version_control_error_when_git_checkout_fails(self, mock_repo):
9297
mock_repo.git.checkout.side_effect = git_utils.git.exc.GitCommandError("git checkout", 255)

0 commit comments

Comments
 (0)