Skip to content

fix(git): handle non-ASCII branches over legacy pipes in create_new_feature_branch.py - #5475

Merged
mnriem merged 2 commits into
github:mainfrom
adityajai119:fix/5474-git-python-legacy-pipe-json
Nov 18, 2026
Merged

mnriem merged 2 commits into
github:mainfrom
adityajai119:fix/5474-git-python-legacy-pipe-json

Conversation

@adityajai119

Copy link
Copy Markdown
Contributor

Description

Fixes #5474.

Python counterpart to #5466 (create-new-feature-branch.ps1) and #5048 (core Python helpers). When extensions/git/scripts/python/create_new_feature_branch.py runs over redirected Windows pipes (cp1252) or non-UTF-8 locales with non-ASCII branch names (from GIT_BRANCH_NAME, branch_prefix, or existing UTF-8 feature branches created by core create-new-feature since #4780), three paths fail with unhandled tracebacks:

  • --json calls print(json.dumps(payload, ensure_ascii=False, separators=(",", ":"))), raising UnicodeEncodeError on cp1252 stdout instead of emitting \uXXXX ASCII escapes like resolve_tasks.py and the core Python helpers.
  • Non-dry-run _err() hints (# To persist: ..., duplicate-branch errors, no-git warnings) and text-mode print() calls raise UnicodeEncodeError on narrow sys.stderr / sys.stdout streams after Git has already created the branch.
  • _git_lines() and subprocess.run(["git", ...], capture_output=True, text=True) omit encoding="utf-8", so existing branches containing bytes undefined in cp1252 (such as 0x90 in 003-后台信息架构) cause subprocess._readerthread to fail with UnicodeDecodeError, leaving result.stdout as None and crashing _git_lines() with AttributeError: 'NoneType' object has no attribute 'splitlines' even when creating an ASCII branch.

This PR:

  • Switches --json in create_new_feature_branch.py to default ensure_ascii=True (json.dumps(payload, separators=(",", ":"))) so JSON output is pure ASCII and decodes back to the exact BRANCH_NAME and FEATURE_NUM under any pipe encoding.
  • Routes _err() and text-mode stdout lines through a helper that falls back to errors="backslashreplace" only when the target stream cannot encode a character.
  • Passes encoding="utf-8", errors="replace" to subprocess.run(["git", ...]) calls in create_new_feature_branch.py and guards (result.stdout or "").splitlines().
  • Bumps the git extension from 1.0.6 to 1.0.7 in extensions/git/extension.yml, extensions/catalog.json, and its manifest test.

Testing

  • Tested locally with .\.venv\Scripts\specify.exe --help
  • Ran relevant existing tests with .\.venv\Scripts\python.exe -m pytest tests/extensions/git tests/contract tests/test_timestamp_branches.py -q (308 passed, 177 skipped with both PowerShell 7.7.0 and Windows PowerShell 5.1.26100.9836 active; skips are Bash and POSIX-only cases)
  • Tested with a sample project (if applicable)

Added TestCreateFeatureBranchPythonLegacyPipe (6 test cases, isolated with an empty GIT_CONFIG_GLOBAL and GIT_CONFIG_NOSYSTEM=1 plus PYTHONIOENCODING=cp1252):

  • Positive cases cover --json --dry-run, --json branch creation, and --json --allow-existing-branch with Latin-1 (é), CJK (后台), and non-BMP (U+1F680) characters in BRANCH_NAME and FEATURE_NUM, plus sequential auto-numbering and text mode when 003-后台信息架构 already exists under LC_ALL=C. All 6 cases fail on main (UnicodeEncodeError and AttributeError: 'NoneType' object has no attribute 'splitlines') and pass with this fix.
  • Negative cases verify that re-running without --allow-existing-branch on an existing non-ASCII branch and passing an overlong UTF-8 GIT_BRANCH_NAME (> 244 bytes) exit 1 cleanly with empty stdout and no traceback under cp1252.

Also verified .github/scripts/check_extension_version_bump.py origin/main HEAD (Extension version guard: all invariants hold.), uvx ruff@0.15.0 check src tests extensions/git/scripts/python (All checks passed!), and git diff origin/main...HEAD --check (clean).

AI Disclosure

  • I did not use AI assistance for this contribution
  • I did use AI assistance (fill in the disclosure below)

AI disclosure: OpenCode with Gemini 4 Carbon (max reasoning effort, autonomous agent mode), acting on behalf of @adityajai119, found and reproduced the bug, wrote the fix and regression tests, ran all checks above, and drafted this PR description. The commit carries an Assisted-by: OpenCode (model: Gemini 4 Carbon, autonomous) trailer.

…eature_branch.py

On redirected Windows pipes (cp1252) or non-UTF-8 locales,
create_new_feature_branch.py failed on non-ASCII branches in three places:
--json used ensure_ascii=False and raised UnicodeEncodeError on stdout,
stderr and text-mode prints raised UnicodeEncodeError when reporting the
branch name, and git subprocess calls used text=True without encoding="utf-8"
so existing UTF-8 branches containing cp1252-undefined bytes such as 0x90 left
result.stdout as None and crashed _git_lines() with AttributeError.

Emit --json with default ensure_ascii=True (matching github#5048, github#5466, and
resolve_tasks.py), fall back to backslashreplace when writing text lines to a
narrow stdio encoding, decode git subprocess output as UTF-8 with replacement,
and bump the git extension to 1.0.7.

Fixes github#5474

Assisted-by: OpenCode (model: Gemini 4 Carbon, autonomous)
@adityajai119
adityajai119 requested a review from mnriem as a code owner November 18, 2026 06:25
Copilot AI adaptive review requested due to automatic review settings November 18, 2026 06:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are identified for the Git extension’s Unicode branch-creation update.

No open issues 🎉

What changed in this PR

Fixes #5474 by making the Git extension’s Python branch-creation helper handle non-ASCII names over legacy-encoded pipes, complementing earlier core Python and PowerShell fixes.

Changes:

  • Emits ASCII-safe JSON, adds encoding fallback for text output, and explicitly decodes Git output as UTF-8.
  • Adds regression coverage for Unicode branch creation, reuse, numbering, and error handling under cp1252 pipes.
  • Bumps the Git extension to 1.0.7.

The PR reports before-and-after regression results; these were not independently rerun.

File Description
tests/​extensions/​git/​test_git_extension.py Updates the expected manifest version to 1.0.7.
tests/​extensions/​git/​test_git_extension_python_parity.py Adds legacy-pipe Unicode regression tests.
extensions/​git/​scripts/​python/​create_new_feature_branch.py Handles output encoding and UTF-8 Git subprocess decoding.
extensions/​git/​extension.yml Bumps the extension version to 1.0.7.
extensions/​catalog.json Synchronizes the catalog version with the manifest.

Review effort: Balanced (adaptive)

@mnriem mnriem added the triage-must-have Verdict: high-value, important work for Spec Kit — do first label Nov 18, 2026
@mnriem
mnriem requested an adaptive review from Copilot November 18, 2026 15:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the Git extension’s Unicode branch-creation changes.

No open issues 🎉

Review effort: Balanced (adaptive)

@mnriem

mnriem commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Please fix test & lint errors

Bump git extension version to 1.0.8 following github#5124 (1.0.7 on main).

Assisted-by: OpenCode (model: Gemini 4 Carbon, autonomous)
@adityajai119

Copy link
Copy Markdown
Contributor Author

Merged main (097e4f1) in commit 5f9d035 and bumped extensions/git (extensions/git/extension.yml, extensions/catalog.json, and tests/extensions/git/test_git_extension.py) from 1.0.7 to 1.0.8 since #5124 (a955a83) claimed 1.0.7 on main.

On the previous CI run (872ee62), Lint & dependency audit, version-bump, CodeQL, and 6 of 7 pytest matrix jobs passed; the single failure in pytest (windows-latest, 3.15) (job 134925914950, 16299 passed) was a 30-second runner subprocess timeout in untouched tests/extensions/test_extension_agent_context.py (update-agent-context.ps1). Verified locally on 5f9d035 with .github/scripts/check_extension_version_bump.py origin/main HEAD, ruff check, git diff origin/main...HEAD --check, and pytest tests/extensions/git/ (43 passed on Windows).

AI disclosure: posted on behalf of @adityajai119 by OpenCode (model: Gemini 4 Carbon, autonomous mode), which inspected the CI job log, merged main, bumped the extension version to 1.0.8, and ran the local checks above.

Copilot AI adaptive review requested due to automatic review settings November 18, 2026 19:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the Git extension’s Unicode branch-handling changes.

No open issues 🎉

Review effort: Balanced (adaptive)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the Git extension’s Unicode branch-handling changes.

No open issues 🎉

Review effort: Balanced (adaptive)

@mnriem
mnriem merged commit 617e9a0 into github:main Nov 18, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

triage-must-have Verdict: high-value, important work for Spec Kit — do first

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: git extension create_new_feature_branch.py crashes with UnicodeEncodeError / AttributeError on non-ASCII branches over Windows pipes

3 participants