Skip to content

Commit

Permalink
Git: delete temp branch before creating if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
mcgov committed Oct 14, 2024
1 parent 2015fd3 commit 38cc77d
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lisa/tools/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,18 @@ 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.
checkout_branch = f"{constants.RUN_ID}"

# 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}",
Expand All @@ -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,
Expand Down

0 comments on commit 38cc77d

Please sign in to comment.