Skip to content

Commit a09bd33

Browse files
committed
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.
1 parent 192d62f commit a09bd33

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/stacky/stacky.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,14 @@ def get_real_stack_bottom() -> Optional[BranchName]: # type: ignore [return]
261261
return candiates.pop()
262262

263263

264+
def get_stack_parent_branch_name(branch: BranchName) -> str:
265+
return "branch.{}.merge".format(branch)
266+
267+
264268
def get_stack_parent_branch(branch: BranchName) -> Optional[BranchName]: # type: ignore [return]
265269
if branch in STACK_BOTTOMS:
266270
return None
267-
p = run(CmdArgs(["git", "config", "branch.{}.merge".format(branch)]), check=False)
271+
p = run(CmdArgs(["git", "config", get_stack_parent_branch_name(branch)]), check=False)
268272
if p is not None:
269273
p = remove_prefix(p, "refs/heads/")
270274
return BranchName(p)
@@ -447,6 +451,12 @@ def load_stack_for_given_branch(
447451
if check:
448452
die("Branch is not in a stack: {}", branch)
449453
return None, [b.branch for b in branches]
454+
if branch == parent:
455+
error("Branch {} seems to be its own parent, this is wrong", branch)
456+
error(
457+
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/<branch>"
458+
)
459+
die("Refusing to continue")
450460
branch = parent
451461

452462
branches.append(BranchNCommit(branch, None))
@@ -1154,7 +1164,7 @@ def set_parent(branch: BranchName, target: BranchName, *, set_origin: bool = Fal
11541164
[
11551165
"git",
11561166
"config",
1157-
"branch.{}.merge".format(branch),
1167+
get_stack_parent_branch_name(branch),
11581168
"refs/heads/{}".format(target),
11591169
]
11601170
)

0 commit comments

Comments
 (0)