better unpushed_commits detection

This commit is contained in:
Waylon Walker 2025-11-25 20:54:39 -06:00
parent ca47d5da89
commit 4de2448aa9

View file

@ -644,30 +644,39 @@ def find_repo_for_worktree(
def has_unpushed_commits(repo_path: Path, branch: str) -> bool:
"""
Detect if branch has commits not on its upstream.
Uses 'git rev-list @{u}..HEAD'; if non-empty, there are unpushed commits.
Detect if `branch` has commits not on its upstream.
If no upstream is configured, we *do not* treat that as "unpushed" anymore,
just "can't check", and return False (so rm won't block on it).
"""
# Check if upstream exists
code, _, _ = run_cmd(
# Find upstream ref, e.g. origin/branch
code, out, _ = run_cmd(
["git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"],
cwd=repo_path,
)
if code != 0:
# No upstream configured; treat as unpushed work.
return True
# No upstream configured; assume "not able to check", *not* a blocker.
return False
upstream = out.strip()
if not upstream or upstream == "@{u}":
return False
# Number of commits on HEAD that are not in upstream
code, out, _ = run_cmd(
["git", "rev-list", "@{u}..HEAD", "--count"],
["git", "rev-list", "--count", f"{upstream}..{branch}"],
cwd=repo_path,
)
if code != 0:
return True
# If we can't check, don't block
return False
try:
count = int(out.strip() or "0")
except ValueError:
return True
return count > 0
return False
return count > 0
def is_branch_integrated_into_main(
repo_path: Path,