diff --git a/lisa/tools/git.py b/lisa/tools/git.py index 35fa778a18..2edc07f75b 100644 --- a/lisa/tools/git.py +++ b/lisa/tools/git.py @@ -103,7 +103,10 @@ def clone( return full_path def checkout( - self, ref: str, cwd: pathlib.PurePath, checkout_branch: str = "" + self, + ref: str, + cwd: pathlib.PurePath, + checkout_branch: str = "", ) -> None: if not checkout_branch: # create a temp branch to checkout tag or commit. @@ -111,7 +114,7 @@ def checkout( # mark directory safe self._mark_safe(cwd) - + branch_before_checkout = self.get_current_branch(cwd=cwd) # force run to make sure checkout among branches correctly. result = self.run( f"checkout {ref}", @@ -120,7 +123,20 @@ def checkout( no_info_log=True, no_error_log=True, ) + # delete old temp branch before checking out new one + if branch_before_checkout == checkout_branch: + self.run( + f"branch -D {branch_before_checkout}", + force_run=True, + cwd=cwd, + no_info_log=True, + no_error_log=True, + ) + result.assert_exit_code( + message=f"failed to delete old temp branch. {result.stdout}" + ) + # create temp branch result = self.run( f"checkout -b {checkout_branch}", force_run=True,