Survive a git branch name that is not UTF-8

This commit is contained in:
2026-10-02 14:23:33 +02:00
parent 0370adf1fc
commit 7ba3d0325b
2 changed files with 7 additions and 6 deletions
+1 -1
View File
@@ -140,7 +140,7 @@ def current_branch(where: str) -> str:
if not where:
return ""
try:
found = subprocess.run(["git", "-C", where, "branch", "--show-current"], capture_output=True, text=True)
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 ""
+6 -5
View File
@@ -103,8 +103,9 @@ class Script(unittest.TestCase):
ran = self.run_script(stdin)
self.assertEqual((ran.returncode, ran.stdout, ran.stderr), (0, f"{RED}warning: statusline.py could not parse its input as a JSON object{RESET}", ""))
def test_shows_the_branch_of_the_directory(self):
with tempfile.TemporaryDirectory() as checkout:
subprocess.run(["git", "init", "-q", "-b", "trunk", checkout], check=True)
ran = self.run_script(json.dumps({"cwd": checkout}))
self.assertEqual(ran.stdout, SEP.join([f"{CYAN}{checkout}{RESET}", f"{DIM}trunk{RESET}"]))
def test_shows_the_branch_of_the_directory_even_when_it_is_not_utf_8(self):
for branch, shown in ((b"trunk", "trunk"), (b"f\xe5", "f\ufffd")):
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}"]), ""))