From 6adf9f6e3cb766e77ddb675aad9d076763fd5e20 Mon Sep 17 00:00:00 2001 From: Bortlesboat <169967362+Bortlesboat@users.noreply.github.com> Date: Thu, 5 Nov 2026 01:17:38 -0500 Subject: [PATCH] fix(extensions): keep git warnings from failing initialize-repo.ps1 on Windows PowerShell 5.1 initialize-repo.ps1 runs git init, git add and git commit with 2>&1 under $ErrorActionPreference = 'Stop' and checks $LASTEXITCODE. Windows PowerShell 5.1 turns every line a native command writes to redirected stderr into a terminating error under 'Stop', even when the command exits 0. With Git for Windows' default core.autocrlf=true, git add warns "LF will be replaced by CRLF" for the LF files Spec Kit writes, so the mandatory before_constitution hook exits 1 after git init but before the initial commit, leaving a stale .git/index.lock. A rerun then skips because the repository already exists. Relax $ErrorActionPreference to 'Continue' around those git calls and restore it afterwards, as auto-commit.ps1 already does (#2258). Failures are still detected through $LASTEXITCODE. PowerShell 7 does not apply $ErrorActionPreference to native stderr, so it was not affected. Bump the git extension to 1.0.4. Assisted-by: Claude Code (model: Claude Opus 5.5, autonomous) --- extensions/catalog.json | 2 +- extensions/git/extension.yml | 2 +- .../scripts/powershell/initialize-repo.ps1 | 7 ++ tests/extensions/git/test_git_extension.py | 89 ++++++++++++++++++- 4 files changed, 94 insertions(+), 6 deletions(-) diff --git a/extensions/catalog.json b/extensions/catalog.json index 0ace624cdc..10ad367461 100644 --- a/extensions/catalog.json +++ b/extensions/catalog.json @@ -51,7 +51,7 @@ "git": { "name": "Git Branching Workflow", "id": "git", - "version": "1.0.3", + "version": "1.0.4", "description": "Feature branch creation, numbering (sequential/timestamp), validation, and Git remote detection", "author": "spec-kit-core", "repository": "https://github.com/github/spec-kit", diff --git a/extensions/git/extension.yml b/extensions/git/extension.yml index cf3f728b77..e8a67a405d 100644 --- a/extensions/git/extension.yml +++ b/extensions/git/extension.yml @@ -3,7 +3,7 @@ schema_version: "1.0" extension: id: git name: "Git Branching Workflow" - version: "1.0.3" + version: "1.0.4" description: "Feature branch creation, numbering (sequential/timestamp), templating, validation, and Git remote detection" author: spec-kit-core repository: https://github.com/github/spec-kit diff --git a/extensions/git/scripts/powershell/initialize-repo.ps1 b/extensions/git/scripts/powershell/initialize-repo.ps1 index fd835f8fe3..b32e24179b 100644 --- a/extensions/git/scripts/powershell/initialize-repo.ps1 +++ b/extensions/git/scripts/powershell/initialize-repo.ps1 @@ -54,6 +54,11 @@ try { } catch { } # Initialize +# Relax ErrorActionPreference so git warnings on stderr (e.g. CRLF notices) +# do not become terminating errors in Windows PowerShell 5.1; real failures +# are still caught through $LASTEXITCODE. +$savedEAP = $ErrorActionPreference +$ErrorActionPreference = 'Continue' try { $out = git init -q 2>&1 | Out-String if ($LASTEXITCODE -ne 0) { throw "git init failed: $out" } @@ -64,6 +69,8 @@ try { } catch { Write-Warning "[specify] Error: $_" exit 1 +} finally { + $ErrorActionPreference = $savedEAP } Write-Host "[OK] Git repository initialized" diff --git a/tests/extensions/git/test_git_extension.py b/tests/extensions/git/test_git_extension.py index 8e1c2652a4..213c5f562d 100644 --- a/tests/extensions/git/test_git_extension.py +++ b/tests/extensions/git/test_git_extension.py @@ -20,6 +20,7 @@ import pytest from tests.conftest import requires_bash +from tests.parity_helpers import WINDOWS_POWERSHELL PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent.parent EXT_DIR = PROJECT_ROOT / "extensions" / "git" @@ -156,12 +157,18 @@ def _run_bash(script_name: str, cwd: Path, *args: str, env_extra: dict | None = ) -def _run_pwsh(script_name: str, cwd: Path, *args: str, env_extra: dict | None = None) -> subprocess.CompletedProcess: - """Run an extension PowerShell script.""" +def _run_pwsh( + script_name: str, + cwd: Path, + *args: str, + env_extra: dict | None = None, + exe: str = "pwsh", +) -> subprocess.CompletedProcess: + """Run an extension PowerShell script (pwsh unless *exe* names another host).""" script = cwd / ".specify" / "extensions" / "git" / "scripts" / "powershell" / script_name env = {**os.environ, **_GIT_ENV, **(env_extra or {})} return subprocess.run( - ["pwsh", "-NoProfile", "-File", str(script), *args], + [exe, "-NoProfile", "-File", str(script), *args], cwd=cwd, capture_output=True, text=True, @@ -179,7 +186,7 @@ def test_manifest_validates(self): m = ExtensionManifest(EXT_DIR / "extension.yml") assert m.id == "git" - assert m.version == "1.0.3" + assert m.version == "1.0.4" def test_manifest_commands(self): """Manifest declares expected commands.""" @@ -319,6 +326,80 @@ def test_skips_if_already_git_repo(self, tmp_path: Path): assert result.returncode == 0 +@pytest.mark.parametrize( + "exe", + [ + pytest.param( + "pwsh", + marks=pytest.mark.skipif(not HAS_PWSH, reason="pwsh not available"), + id="pwsh", + ), + pytest.param( + WINDOWS_POWERSHELL, + marks=pytest.mark.skipif( + not WINDOWS_POWERSHELL, reason="Windows PowerShell 5.1 not available" + ), + id="windows-powershell-5.1", + ), + ], +) +class TestInitializeRepoPowerShellStderr: + """git warnings on stderr must not fail initialize-repo.ps1. + + With $ErrorActionPreference = 'Stop', Windows PowerShell 5.1 turns every + line a native command writes to redirected stderr into a terminating + error, even when the command exits 0. PowerShell 7 does not, so only the + windows-powershell-5.1 case can reproduce it. + """ + + def test_crlf_warnings_do_not_abort_initial_commit(self, tmp_path: Path, exe: str): + """LF files with core.autocrlf=true still get the initial commit.""" + project = _setup_project(tmp_path / "proj", git=False) + (project / "notes.txt").write_bytes(b"line one\nline two\n") + env = { + "GIT_CONFIG_COUNT": "1", + "GIT_CONFIG_KEY_0": "core.autocrlf", + "GIT_CONFIG_VALUE_0": "true", + } + + result = _run_pwsh("initialize-repo.ps1", project, env_extra=env, exe=exe) + + assert result.returncode == 0, result.stdout + result.stderr + assert "[OK] Git repository initialized" in result.stdout + log = subprocess.run( + ["git", "log", "--format=%s"], + cwd=project, capture_output=True, text=True, + ) + assert log.stdout.strip() == "[Spec Kit] Initial commit" + + def test_commit_failure_is_still_reported(self, tmp_path: Path, exe: str): + """A failing git commit still exits 1 and leaves no commit behind.""" + project = _setup_project(tmp_path / "proj", git=False) + hooks = tmp_path / "hooks" + hooks.mkdir() + hook = hooks / "pre-commit" + hook.write_bytes(b'#!/bin/sh\necho "pre-commit: rejected" >&2\nexit 1\n') + hook.chmod(0o755) + # autocrlf=false keeps the hook's message the only stderr output. + env = { + "GIT_CONFIG_COUNT": "2", + "GIT_CONFIG_KEY_0": "core.autocrlf", + "GIT_CONFIG_VALUE_0": "false", + "GIT_CONFIG_KEY_1": "core.hooksPath", + "GIT_CONFIG_VALUE_1": str(hooks), + } + + result = _run_pwsh("initialize-repo.ps1", project, env_extra=env, exe=exe) + + assert result.returncode == 1 + assert "pre-commit: rejected" in result.stdout + result.stderr + commits = subprocess.run( + ["git", "rev-list", "--all", "--count"], + cwd=project, capture_output=True, text=True, + ) + assert commits.stdout.strip() == "0" + + # ── create-new-feature-branch.sh Tests ──────────────────────────────────────────────