Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(git_refs): resolve default_base from full refs (#4388)

Mayuriecb00e0e25a7
diff --git a/packages/core/src/repowise/core/git_refs.py b/packages/core/src/repowise/core/git_refs.py
index 5b259d0ba..06cafec33 100644
--- a/packages/core/src/repowise/core/git_refs.py
+++ b/packages/core/src/repowise/core/git_refs.py
@@ -141,12 +141,15 @@ def resolve(repo_path: str, rev: str) -> str:
def default_base(repo_path: str) -> str:
"""The branch a change is most likely cut from, else ``HEAD``."""
- head = _read(repo_path, ["symbolic-ref", "--short", "refs/remotes/origin/HEAD"])
- if head:
- return head
+ head = _read(repo_path, ["symbolic-ref", "refs/remotes/origin/HEAD"])
+ remotes_prefix = "refs/remotes/"
+ if head.startswith(remotes_prefix):
+ return head[len(remotes_prefix) :]
for name in _FALLBACK_BASES:
- if resolve(repo_path, name):
+ if resolve(repo_path, f"refs/heads/{name}"):
return name
+ if resolve(repo_path, f"refs/remotes/origin/{name}"):
+ return f"origin/{name}"
return "HEAD"
diff --git a/tests/unit/test_git_refs.py b/tests/unit/test_git_refs.py
index bb40d747c..80b545a5e 100644
--- a/tests/unit/test_git_refs.py
+++ b/tests/unit/test_git_refs.py
@@ -124,6 +124,26 @@ class TestDefaultBase:
def test_a_non_repository_is_head(self, tmp_path: Path) -> None:
assert default_base(str(tmp_path / "nowhere_at_all")) == "HEAD"
+ def test_a_local_branch_named_like_the_remote_one_does_not_change_the_answer(self, repo: Path) -> None:
+ _run(repo, "update-ref", "refs/remotes/origin/trunk", "main")
+ _run(repo, "symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/trunk")
+ _run(repo, "branch", "origin/trunk", "main")
+ assert default_base(str(repo)) == "origin/trunk"
+
+ def test_a_tag_named_like_a_trunk_is_not_a_base(self, tmp_path: Path) -> None:
+ root = tmp_path / "tagged"
+ root.mkdir()
+ _run(root, "init", "-b", "topic")
+ _commit(root, "a.txt", "a\n", "a")
+ _run(root, "tag", "main")
+ assert default_base(str(root)) == "HEAD"
+
+ def test_falls_back_to_a_remote_trunk_when_there_is_no_local_one(self, repo: Path) -> None:
+ _run(repo, "symbolic-ref", "-d", "refs/remotes/origin/HEAD")
+ _run(repo, "checkout", "-q", "feat")
+ _run(repo, "branch", "-D", "main")
+ assert default_base(str(repo)) == "origin/main"
+
class TestDefaultBranch:
def test_is_the_origin_head_target_without_the_remote(self, repo: Path) -> None: