fix: strip remote prefix from branch names in list_branches
Git branch -a --format=%(refname:short) returns remote branches as 'origin/branch-name', not 'remotes/origin/branch-name'. The code was only filtering 'remotes/' prefix, causing clone to fail with branch names like 'origin/feat/foo'. Now properly detects remote names using 'git remote' and strips the remote prefix (e.g., 'origin/') from branch names.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -289,19 +289,37 @@ def list_branches(repo_path: str) -> tuple[list[BranchInfo], str]:
|
|||||||
branches: list[BranchInfo] = []
|
branches: list[BranchInfo] = []
|
||||||
default_branch = "main"
|
default_branch = "main"
|
||||||
|
|
||||||
|
# Get list of remote names to properly filter remote tracking branches
|
||||||
|
try:
|
||||||
|
remote_output = _run_git_command(repo_path, "remote")
|
||||||
|
remote_names = {r.strip() for r in remote_output.strip().split("\n") if r.strip()}
|
||||||
|
except RuntimeError:
|
||||||
|
remote_names = set()
|
||||||
|
|
||||||
for line in output.strip().split("\n"):
|
for line in output.strip().split("\n"):
|
||||||
if not line:
|
if not line:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
branch_name = line.strip()
|
branch_name = line.strip()
|
||||||
# Skip remote tracking branches (they start with remotes/)
|
|
||||||
if branch_name.startswith("remotes/"):
|
# Skip detached HEAD pointer
|
||||||
# Extract just the branch name part
|
if branch_name == "HEAD":
|
||||||
parts = branch_name.split("/", 2)
|
continue
|
||||||
if len(parts) >= 3:
|
|
||||||
branch_name = parts[2]
|
# Skip remote tracking branches - they appear as "origin/branch-name"
|
||||||
else:
|
# Check if first part is a remote name
|
||||||
continue
|
if "/" in branch_name:
|
||||||
|
first_part = branch_name.split("/", 1)[0]
|
||||||
|
if first_part in remote_names:
|
||||||
|
# Extract just the branch name part (after "origin/")
|
||||||
|
branch_name = branch_name.split("/", 1)[1]
|
||||||
|
elif branch_name.startswith("remotes/"):
|
||||||
|
# Handle "remotes/origin/branch-name" format
|
||||||
|
parts = branch_name.split("/", 2)
|
||||||
|
if len(parts) >= 3:
|
||||||
|
branch_name = parts[2]
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
|
||||||
# Skip duplicates
|
# Skip duplicates
|
||||||
if any(b.name == branch_name for b in branches):
|
if any(b.name == branch_name for b in branches):
|
||||||
|
|||||||
Reference in New Issue
Block a user