Repository navigation
Preserve Unicode Git and GitHub names under legacy locales #1907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6b64e2d
1054ab1
098b63b
c6f7279
487048f
75c9f70
56fc575
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -390,7 +390,7 @@ def _gh(*args: str, cwd: Path | None = None, timeout: float = 120, | |
| try: | ||
| done = run(list(argv), cwd=cwd, env_allowlist=GH_ENV, | ||
| extra_env={"GH_TOKEN": selected} if selected else None, | ||
| timeout=_read_timeout(timeout, scoped=read_only)) | ||
| timeout=_read_timeout(timeout, scoped=read_only), encoding="utf-8") | ||
| except ToolMissing as exc: | ||
| raise GhMissing(argv, str(exc), token=selected, | ||
| guidance=f"\nInstall GitHub CLI, then {GH_AUTH_REMEDY}") from exc | ||
|
|
@@ -1516,7 +1516,7 @@ def delete_remote_branch(repo: str, branch: str, expected_head: str) -> bool: | |
| current = _remote_branch_head(endpoint) | ||
| if current != expected_head: | ||
| return current is None | ||
| ref = f"refs/heads/{branch}" | ||
| ref = os.fsdecode(f"refs/heads/{branch}".encode("utf-8", "surrogateescape")) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: No test runs The legacy-locale test calls Add a narrow check for this path. One option: monkeypatch |
||
| argv = ("git", "-c", "credential.helper=!gh auth git-credential", | ||
| "push", f"--force-with-lease={ref}:{expected_head}", | ||
| f"https://github.com/{repo}.git", f":{ref}") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -189,8 +189,8 @@ def run(argv, *, cwd, env_allowlist, extra_env=None, **kwargs): | |
|
|
||
|
|
||
| @pytest.fixture | ||
| def wt(repo: Path, git_seed: Path, monkeypatch: pytest.MonkeyPatch) -> Path: | ||
| path = cli.worktree.worktree_dir(repo, "t1") | ||
| def wt(repo: Path, git_seed: Path, monkeypatch: pytest.MonkeyPatch, request) -> Path: | ||
| path = cli.worktree.worktree_dir(repo, getattr(request, "param", "t1")) | ||
| git(repo, "worktree", "add", "-q", str(path), "lz/t1") | ||
| shutil.copytree(git_seed / ".loopzero", cli.worktree.task_file(path).parent) | ||
| monkeypatch.chdir(path) | ||
|
|
@@ -1353,6 +1353,47 @@ def test_pr_uses_lease_for_rewritten_open_pr(wt, repo, gh, fake_tool, tmp_path, | |
| assert git(origin, "rev-parse", "refs/heads/lz/t1").strip() == (old if case == "hook-fails" else head) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("wt", ["Łódź"], indirect=True) | ||
| def test_legacy_locale_status_reflog_push_and_cleanup(wt, repo, gh, monkeypatch, rewritten_branch): | ||
| branch, head = "lz/Łódź", head_of(wt) | ||
| ref, origin = f"refs/heads/{branch}", repo.parent / "origin.git" | ||
| git(wt, "branch", "-m", branch) | ||
| git(origin, "update-ref", ref, rewritten_branch) | ||
| assert git(wt, "remote", "get-url", "origin").strip() == str(origin) | ||
| monkeypatch.chdir(repo) | ||
|
|
||
| gh.respond(branch_history_key(branch=branch), []) | ||
| gh.respond_pr_list([pr_json(headRefOid=rewritten_branch)], branch=branch) | ||
| gh.respond(reviews_key(), []) | ||
| gh.respond(f"api repos/{REPO}/pulls/7 --method PATCH", {}) | ||
| program = """ | ||
| from pathlib import Path | ||
| from loopzero import cli, worktree | ||
|
|
||
| cli._readiness = lambda *_, **__: cli.github.Readiness(False, ("check \\u0141 is pending",)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Legacy-locale test stubs out The subprocess replaces |
||
| assert cli.main(["status"]) == 0 | ||
| # The fixture verifies and uses a local bare remote. | ||
| cli._require_push_target = lambda *_: None | ||
| assert cli.main(["pr"]) == 0 | ||
| wt = Path.cwd() | ||
| head = worktree.head(wt) | ||
| cli._delete_tracking_ref(wt, worktree.branch(wt), head) | ||
| worktree.cleanup(wt, head) | ||
| """ | ||
| result = subprocess.run( | ||
| [sys.executable, "-X", "utf8=0", "-c", program], cwd=wt, | ||
| env={**os.environ, "LC_ALL": "C", "PYTHONCOERCECLOCALE": "0", "PYTHONIOENCODING": "ascii:strict"}, | ||
| capture_output=True, encoding="ascii", | ||
| ) | ||
| assert result.returncode == 0, result.stderr | ||
| escaped = branch.encode("ascii", "backslashreplace").decode("ascii") | ||
| assert f"branch: {escaped}" in result.stdout | ||
| assert r"check \u0141 is pending" in result.stdout | ||
| assert any(pr_list_key(branch).removeprefix("api ") in call["argv"] for call in gh.calls) | ||
| assert git(origin, "rev-parse", ref).strip() == head | ||
| assert not wt.exists() and not git(repo, "branch", "-a", "--list", f"*{branch}") | ||
|
|
||
|
|
||
| def test_pr_refuses_to_overwrite_a_pr_head_it_has_never_seen( | ||
| wt: Path, repo: Path, gh: FakeGh, capsys | ||
| ) -> None: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Unicode↔Git-native name conversion is repeated by hand in five places
The same idiom is written out at five sites:
os.fsdecode(x.encode("utf-8", "surrogateescape"))at cli.py:712, cli.py:1853 and github.py:1479.os.fsencode(x).decode("utf-8", "surrogateescape"), at worktree.py:308 and worktree.py:355.branch()now returns either form depending on thenativeflag, so every new Git call that takes a task-branch name must remember to convert it. If one forgets, it crashes with UnicodeEncodeError under a legacy locale.The same gap already exists for the configured base branch. The TOML-sourced
base_branchreaches Git argv unconverted at config.py:75, 106 and 109, worktree.py:231 and cli.py:1277. These are outside this task's acceptance, but they belong to the same class.Suggested fix: add a pair of helpers in worktree, for example
git_name(unicode) -> nativeandunicode_name(native) -> unicode, and use them at all of these sites. That keeps the conversion in one place.