Skip to content

Commit 0c08ff1

Browse files
fix(git-ext): honor explicit -Number 0 in PowerShell branch creation
`-Number` defaults to 0, so the previous `-eq 0` / `-ne 0` checks could not distinguish an unset flag from an explicit `-Number 0`: a user requesting branch `000-...` was silently routed into auto-detection. Switch both checks to `$PSBoundParameters.ContainsKey('Number')`, which tests whether the flag was actually supplied — mirroring the bash twin's empty-string sentinel (`[ -z "$BRANCH_NUMBER" ]` / `[ -n ... ]`). Add a parity regression test to both TestCreateFeatureBash and TestCreateFeaturePowerShell asserting `--number 0` / `-Number 0` yields `000-zero`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent a7b4391 commit 0c08ff1

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

extensions/git/scripts/powershell/create-new-feature-branch.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,10 @@ if ($env:GIT_BRANCH_NAME) {
446446
$branchSuffix = Get-BranchName -Description $featureDesc
447447
}
448448

449-
if ($Timestamp -and $Number -ne 0) {
449+
# Warn if -Number and -Timestamp are both specified. Use ContainsKey (not
450+
# `-ne 0`) so an explicit `-Number 0` is also detected, matching the bash twin's
451+
# `[ -n "$BRANCH_NUMBER" ]` check.
452+
if ($Timestamp -and $PSBoundParameters.ContainsKey('Number')) {
450453
Write-Warning "[specify] Warning: -Number is ignored when -Timestamp is used"
451454
$Number = 0
452455
}
@@ -456,7 +459,10 @@ if ($env:GIT_BRANCH_NAME) {
456459
$branchName = New-BranchName -FeatureNum $featureNum -BranchSuffix $branchSuffix
457460
} else {
458461
$branchScopePrefix = Get-BranchScopePrefix -Template $branchTemplate -BranchSuffix $branchSuffix
459-
if ($Number -eq 0) {
462+
# Auto-detect the next number only when -Number was not supplied; an
463+
# explicit value (including 0) is honored, matching the bash twin's
464+
# `[ -z "$BRANCH_NUMBER" ]` check.
465+
if (-not $PSBoundParameters.ContainsKey('Number')) {
460466
if ($DryRun -and $hasGit) {
461467
$Number = Get-NextBranchNumber -SpecsDir $specsDir -SkipFetch -ScopePrefix $branchScopePrefix
462468
} elseif ($DryRun) {

tests/extensions/git/test_git_extension.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,21 @@ def test_specify_init_dir_with_stale_core_errors(self, tmp_path: Path):
638638
assert result.returncode != 0
639639
assert "requires updated Spec Kit core scripts" in result.stderr
640640

641+
def test_explicit_number_zero_is_honored(self, tmp_path: Path):
642+
"""An explicit --number 0 is honored (yields 000), not treated as
643+
'auto-detect'. Pins the canonical behavior the PowerShell twin must
644+
mirror; the empty-string check (`[ -z "$BRANCH_NUMBER" ]`) already
645+
distinguishes an unset flag from a supplied 0."""
646+
project = _setup_project(tmp_path)
647+
result = _run_bash(
648+
"create-new-feature-branch.sh", project,
649+
"--json", "--dry-run", "--number", "0", "--short-name", "zero", "Zero feature",
650+
)
651+
assert result.returncode == 0, result.stderr
652+
data = json.loads(result.stdout)
653+
assert data["BRANCH_NAME"] == "000-zero"
654+
assert data["FEATURE_NUM"] == "000"
655+
641656

642657
@pytest.mark.skipif(not HAS_PWSH, reason="pwsh not available")
643658
class TestCreateFeaturePowerShell:
@@ -942,6 +957,23 @@ def test_specify_init_dir_with_stale_core_errors(self, tmp_path: Path):
942957
assert result.returncode != 0
943958
assert "requires updated Spec Kit core scripts" in result.stderr
944959

960+
def test_explicit_number_zero_is_honored(self, tmp_path: Path):
961+
"""An explicit -Number 0 is honored (yields 000), matching the bash twin's
962+
--number 0. Regression guard: -Number defaults to 0, so a bare `-eq 0`
963+
check cannot tell an unset flag from a supplied 0 and would silently
964+
auto-detect instead. Uses PSBoundParameters.ContainsKey('Number')."""
965+
project = _setup_project(tmp_path)
966+
result = _run_pwsh(
967+
"create-new-feature-branch.ps1", project,
968+
"-Json", "-DryRun", "-Number", "0", "-ShortName", "zero", "Zero feature",
969+
)
970+
assert result.returncode == 0, result.stderr
971+
json_line = [ln for ln in result.stdout.splitlines() if ln.strip().startswith("{")]
972+
assert json_line, f"No JSON in output: {result.stdout}"
973+
data = json.loads(json_line[-1])
974+
assert data["BRANCH_NAME"] == "000-zero"
975+
assert data["FEATURE_NUM"] == "000"
976+
945977

946978
# ── auto-commit.sh Tests ─────────────────────────────────────────────────────
947979

0 commit comments

Comments
 (0)