Skip to content

Commit 2b92a0d

Browse files
committed
refactor(release): reset hard to remote branch on checkout if it exists locally
1 parent 75101fc commit 2b92a0d

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

tests/tools/private/release/release_test.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import unittest
77
from unittest.mock import MagicMock, call, patch
88

9-
from tools.private.release import changelog_news, release as releaser, utils
9+
from tools.private.release import changelog_news, git, release as releaser, utils
1010
from tools.private.release.gh import MultipleTrackingIssuesError, NoTrackingIssueError
1111

1212

@@ -1676,5 +1676,42 @@ def mock_resolve(items):
16761676
self.mock_git.push.assert_not_called()
16771677

16781678

1679+
class GitCheckoutTest(unittest.TestCase):
1680+
@patch("tools.private.release.git.run_cmd")
1681+
def test_checkout_simple(self, mock_run_cmd):
1682+
git.checkout("my-branch")
1683+
mock_run_cmd.assert_called_once_with(
1684+
"git", "checkout", "my-branch", capture_output=False
1685+
)
1686+
1687+
@patch("tools.private.release.git.branch_exists")
1688+
@patch("tools.private.release.git.run_cmd")
1689+
def test_checkout_track_remote_new_branch(self, mock_run_cmd, mock_branch_exists):
1690+
mock_branch_exists.return_value = False
1691+
1692+
git.checkout("my-branch", track_remote="origin")
1693+
1694+
mock_branch_exists.assert_called_once_with("my-branch")
1695+
mock_run_cmd.assert_called_once_with(
1696+
"git", "checkout", "--track", "origin/my-branch", capture_output=False
1697+
)
1698+
1699+
@patch("tools.private.release.git.reset_hard")
1700+
@patch("tools.private.release.git.branch_exists")
1701+
@patch("tools.private.release.git.run_cmd")
1702+
def test_checkout_track_remote_existing_branch(
1703+
self, mock_run_cmd, mock_branch_exists, mock_reset_hard
1704+
):
1705+
mock_branch_exists.return_value = True
1706+
1707+
git.checkout("my-branch", track_remote="origin")
1708+
1709+
mock_branch_exists.assert_called_once_with("my-branch")
1710+
mock_run_cmd.assert_called_once_with(
1711+
"git", "checkout", "my-branch", capture_output=False
1712+
)
1713+
mock_reset_hard.assert_called_once_with("origin/my-branch")
1714+
1715+
16791716
if __name__ == "__main__":
16801717
unittest.main()

tools/private/release/git.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,21 @@ def checkout(
2525
cmd = ["git", "checkout"]
2626
if create_branch:
2727
cmd.append("-b")
28+
29+
should_reset_hard = False
2830
if track_remote:
2931
if branch_exists(ref):
3032
cmd.append(ref)
33+
should_reset_hard = True
3134
else:
3235
cmd.extend(["--track", f"{track_remote}/{ref}"])
3336
else:
3437
cmd.append(ref)
3538
run_cmd(*cmd, capture_output=False)
3639

40+
if should_reset_hard:
41+
reset_hard(f"{track_remote}/{ref}")
42+
3743

3844
def add(*files):
3945
"""Stages files for commit."""

0 commit comments

Comments
 (0)