Skip to content

Commit 60eae56

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 6315a1e commit 60eae56

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
@@ -271,10 +271,14 @@ def get_real_stack_bottom() -> Optional[BranchName]: # type: ignore [return]
271271
return candiates.pop()
272272

273273

274+
def get_stack_parent_branch_name(branch: BranchName) -> str:
275+
return "branch.{}.merge".format(branch)
276+
277+
274278
def get_stack_parent_branch(branch: BranchName) -> Optional[BranchName]: # type: ignore [return]
275279
if branch in STACK_BOTTOMS:
276280
return None
277-
p = run(CmdArgs(["git", "config", "branch.{}.merge".format(branch)]), check=False)
281+
p = run(CmdArgs(["git", "config", get_stack_parent_branch_name(branch)]), check=False)
278282
if p is not None:
279283
p = remove_prefix(p, "refs/heads/")
280284
if BranchName(p) == branch:
@@ -482,6 +486,12 @@ def load_stack_for_given_branch(
482486
if check:
483487
die("Branch is not in a stack: {}", branch)
484488
return None, [b.branch for b in branches]
489+
if branch == parent:
490+
error("Branch {} seems to be its own parent, this is wrong", branch)
491+
error(
492+
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>"
493+
)
494+
die("Refusing to continue")
485495
branch = parent
486496

487497
branches.append(BranchNCommit(branch, None))
@@ -1241,7 +1251,7 @@ def set_parent(branch: BranchName, target: Optional[BranchName], *, set_origin:
12411251
[
12421252
"git",
12431253
"config",
1244-
"branch.{}.merge".format(branch),
1254+
get_stack_parent_branch_name(branch),
12451255
"refs/heads/{}".format(target if target is not None else branch),
12461256
]
12471257
)

0 commit comments

Comments
 (0)