feat(extensions): port git extension scripts to Python#3400
feat(extensions): port git extension scripts to Python#3400marcelsafin wants to merge 3 commits into
Conversation
Ports git-common, initialize-repo, auto-commit, and create-new-feature-branch to extensions/git/scripts/python/, mirroring the bash/PowerShell twins. Parity tests run each bash script and its Python twin in identical projects and compare output, exit codes, and resulting git state. Fixes github#3282 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR adds Python equivalents for the extensions/git workflow scripts, aiming to preserve behavior while reducing dual-maintenance across bash/PowerShell. It also introduces a parity-focused test suite to validate the Python ports against the existing bash implementations.
Changes:
- Added Python ports for
create-new-feature-branch,initialize-repo,auto-commit, and shared git helpers. - Introduced a parity test suite that runs bash vs Python “twin projects” and compares outcomes.
- Added unit tests for
git_common.pyhelper behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/extensions/git/test_git_extension_python_parity.py | Adds parity tests (bash vs Python) plus unit tests for git_common.py. |
| extensions/git/scripts/python/git_common.py | Implements git detection + feature-branch validation helpers. |
| extensions/git/scripts/python/initialize_repo.py | Initializes a repo and makes an initial commit using optional config-driven message. |
| extensions/git/scripts/python/auto_commit.py | Parses git-config.yml auto-commit settings and commits changes per event. |
| extensions/git/scripts/python/create_new_feature_branch.py | Implements feature-branch naming/numbering/template logic and branch creation, mirroring bash behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Configuration: | ||
| branch_template Optional git-config.yml template with {{author}}, {{app}}, {{number}}, {{slug}} | ||
| branch_prefix Optional shorthand namespace expanded before {{number}}-{{slug}} | ||
|
|
There was a problem hiding this comment.
The help text renders correctly: HELP_TEXT is an f-string, so {{number}} in the source emits {number} at runtime. Verified: python3 create_new_feature_branch.py --help prints "template with {author}, {app}, {number}, {slug}".
| feature_description = " ".join(args.description_parts).strip() | ||
| if not feature_description: | ||
| _err(USAGE) | ||
| return 1 |
There was a problem hiding this comment.
Fixed in ee4f9c8. The script now mirrors the bash order: usage error for missing description, then the specific whitespace message after trimming.
| def test_empty_description_errors(self, tmp_path: Path): | ||
| bash_proj, py_proj = _twin_projects(tmp_path) | ||
| b = _run_bash("create-new-feature-branch.sh", bash_proj, "--json", " ") | ||
| p = _run_py("create-new-feature-branch", py_proj, "--json", " ") | ||
| assert b.returncode == p.returncode == 1 | ||
|
|
There was a problem hiding this comment.
Done in ee4f9c8. The test now asserts stderr parity and the specific error message.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
| def _assert_parity( | ||
| bash_result: subprocess.CompletedProcess, | ||
| py_result: subprocess.CompletedProcess, | ||
| *, | ||
| stdout: bool = True, | ||
| ) -> None: | ||
| assert py_result.returncode == bash_result.returncode, ( | ||
| f"exit codes diverge: bash={bash_result.returncode} py={py_result.returncode}\n" | ||
| f"bash stderr: {bash_result.stderr}\npy stderr: {py_result.stderr}" | ||
| ) | ||
| if stdout: | ||
| assert py_result.stdout == bash_result.stdout |
There was a problem hiding this comment.
Agreed. _assert_parity now compares stderr by default alongside exit code and stdout. All existing parity tests pass with the stricter assertion, so no call sites needed an opt-out. 5241529
| in_auto_commit = False | ||
| in_event = False | ||
|
|
||
| content = config_file.read_text(encoding="utf-8") |
There was a problem hiding this comment.
Fixed. read_text is now wrapped in try/except OSError and returns (False, ""), so an unreadable config disables auto-commit the same way a missing one does. Added test_unreadable_config_skips_auto_commit (chmod 000, asserts exit 0, no traceback, no commit). 5241529
An unreadable config file raised OSError with a full traceback from _parse_auto_commit_config. Treat it like a missing config: auto-commit stays disabled. Covered by a chmod-000 test (skipped on non-POSIX and as root). _assert_parity now also compares stderr so warning or usage-text regressions between the bash and Python twins fail the suite. All existing parity tests pass with the stricter assertion. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Summary
Fixes #3282 — part of #3277. Last script port in the porting phase:
extensions/git/scripts/{bash,powershell}/now has Python twins inextensions/git/scripts/python/.Changes
git_common.py—has_git,effective_branch_name,check_feature_branch(port of git-common.sh)initialize_repo.py— repo init with configurable initial commit messageauto_commit.py— per-event auto-commit, mirroring the line-based git-config.yml parser including the default/explicit-false precedence rulescreate_new_feature_branch.py— full port: slug generation with stop-word filtering and acronym handling, sequential numbering across specs/local branches/remote refs,branch_template/branch_prefixwith scoped numbering,GIT_BRANCH_NAMEoverride, 244-byte truncation,SPECIFY_INIT_DIRvia the corecommon.pyresolver (same refusal as bash when core scripts are missing)Same conventions as the merged check-prerequisites PoC and the #3280/#3281 ports: stdlib only, standalone scripts, no runtime wiring changes.
Testing
43 new tests in
tests/extensions/git/test_git_extension_python_parity.py. The parity tests run each bash script and its Python twin in identical twin projects and assert matching stdout, exit codes, stderr, and resulting git state (branches created, commit messages). Covers branch creation and commit behavior per the issue's acceptance criteria, plus template validation errors, existing-branch handling, and no-git degradation.uv run pytest tests/extensions/git/— 97 passeduvx ruff check— cleanChecklist