From 60eae56d3bbbaf1f056315e8768cd78422470b69 Mon Sep 17 00:00:00 2001 From: Matthieu Patou Date: Fri, 20 Sep 2024 14:04:57 -0700 Subject: [PATCH] Detect when the parent is the same as current branch We encounter sometime loops in stacky when the parent is the same as branch and we are caught in an infinite loop. --- src/stacky/stacky.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/stacky/stacky.py b/src/stacky/stacky.py index 9da39aa..830a9d4 100755 --- a/src/stacky/stacky.py +++ b/src/stacky/stacky.py @@ -271,10 +271,14 @@ def get_real_stack_bottom() -> Optional[BranchName]: # type: ignore [return] return candiates.pop() +def get_stack_parent_branch_name(branch: BranchName) -> str: + return "branch.{}.merge".format(branch) + + def get_stack_parent_branch(branch: BranchName) -> Optional[BranchName]: # type: ignore [return] if branch in STACK_BOTTOMS: return None - p = run(CmdArgs(["git", "config", "branch.{}.merge".format(branch)]), check=False) + p = run(CmdArgs(["git", "config", get_stack_parent_branch_name(branch)]), check=False) if p is not None: p = remove_prefix(p, "refs/heads/") if BranchName(p) == branch: @@ -482,6 +486,12 @@ def load_stack_for_given_branch( if check: die("Branch is not in a stack: {}", branch) return None, [b.branch for b in branches] + if branch == parent: + error("Branch {} seems to be its own parent, this is wrong", branch) + error( + f"To fix it update the config for {get_stack_parent_branch_name(branch)} by running git config {get_stack_parent_branch_name(branch)} ref/heads/" + ) + die("Refusing to continue") branch = parent branches.append(BranchNCommit(branch, None)) @@ -1241,7 +1251,7 @@ def set_parent(branch: BranchName, target: Optional[BranchName], *, set_origin: [ "git", "config", - "branch.{}.merge".format(branch), + get_stack_parent_branch_name(branch), "refs/heads/{}".format(target if target is not None else branch), ] )