Read the status line's branch through git_repo.git, and state the comprehension-floor rule as it holds

This commit is contained in:
2026-10-22 19:43:26 +02:00
parent 52ba0591fa
commit 2fefab6b2e
5 changed files with 13 additions and 21 deletions
+5 -6
View File
@@ -4,9 +4,11 @@ import datetime
import json
import math
import os
import subprocess
import sys
from failure import Failure
import git_repo
CYAN = "\033[36m"
DIM = "\033[2m"
GREEN = "\033[32m"
@@ -141,11 +143,8 @@ def render(status: Status, branch: str, home: str, now: datetime.datetime) -> st
def current_branch(where: str) -> str:
if not where:
return ""
try:
found = subprocess.run(["git", "-C", where, "branch", "--show-current"], errors="replace", stderr=subprocess.DEVNULL, stdout=subprocess.PIPE, text=True)
except OSError:
return ""
return found.stdout.strip() if found.returncode == 0 else ""
found = git_repo.git("branch", "--show-current", cwd=where)
return "" if isinstance(found, Failure) else found.strip()
def main() -> str:
+5 -3
View File
@@ -103,9 +103,11 @@ class Script(unittest.TestCase):
ran = self.run_script(stdin)
self.assertEqual((ran.returncode, ran.stdout, ran.stderr), (0, f"{RED}warning: statusline.py's input is not a JSON object{RESET}", ""))
def test_shows_a_branch_name_that_is_not_utf_8(self):
for branch, shown in ((b"trunk", "trunk"), (b"f\xe5", "f\ufffd")):
def test_shows_no_branch_where_git_fails(self):
# git_repo.git fails on text that is not UTF-8, so a branch named f\xe5 shows as none.
for branch, shown in ((b"trunk", "trunk"), (b"f\xe5", None)):
with tempfile.TemporaryDirectory() as checkout:
subprocess.run([b"git", b"init", b"-q", b"-b", branch, checkout.encode()], check=True)
ran = self.run_script(json.dumps({"cwd": checkout}))
self.assertEqual((ran.stdout, ran.stderr), (SEP.join([f"{CYAN}{checkout}{RESET}", f"{DIM}{shown}{RESET}"]), ""))
row = [f"{CYAN}{checkout}{RESET}"] + ([] if shown is None else [f"{DIM}{shown}{RESET}"])
self.assertEqual((ran.stdout, ran.stderr), (SEP.join(row), ""))