Skip to content

Commit fecf719

Browse files
committed
ap_git: force-create temp branch while shallow cloning from local repo
Any failure between `__branch_create` and `__branch_delete` can leave behind a stale temp branch, which can cause failures when we try to create a branch with the same name in subsequent builds. Since `shallow_clone_at_commit_from_local` is the only function that creates these temp branches and expects the temp branch to always track the required commit, we can force-create it to handle cases where a stale branch is left behind from a previous failure.
1 parent 57cde47 commit fecf719

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

ap_git/core.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,19 +276,25 @@ def fetch_remote(self, remote: str, force: bool = False,
276276
subprocess.run(cmd, cwd=self.__local_path, shell=False)
277277

278278
def __branch_create(self, branch_name: str,
279-
start_point: str = None) -> None:
279+
start_point: str = None,
280+
force: bool = False) -> None:
280281
"""
281282
Create a new branch starting from a given commit.
282283
283284
Parameters:
284285
branch_name (str): Name of the branch to create
285286
start_point (str): Starting commit or branch (optional)
287+
force (bool): Force creation, resetting the branch tip if it
288+
already exists (default is False)
286289
"""
287290
if branch_name is None:
288291
raise ValueError("branch_name is required, cannot be None.")
289292

290293
cmd = ['git', 'branch', branch_name]
291294

295+
if force:
296+
cmd.append('-f')
297+
292298
if start_point:
293299
if not self.__is_commit_present_locally(commit_ref=start_point):
294300
raise ex.CommitNotFoundError(commit_ref=start_point)
@@ -626,7 +632,7 @@ def shallow_clone_at_commit_from_local(source: str,
626632
# as shallow clone needs a branch
627633
temp_branch_name = "temp-b-" + commit_id
628634
source_repo.__branch_create(
629-
branch_name=temp_branch_name, start_point=commit_id
635+
branch_name=temp_branch_name, start_point=commit_id, force=True
630636
)
631637

632638
# perform the clone from the source repository

0 commit comments

Comments
 (0)