Skip to content

Commit 3b2ba61

Browse files
jawwad-aliclaude
andcommitted
fix(scripts): honor explicit -Number 0 in PowerShell create-new-feature (parity with bash)
Get-BranchName used `[long]$Number = 0` as both the default and the 'auto-detect' sentinel (`if ($Number -eq 0)`), so an explicitly-passed `-Number 0` was indistinguishable from 'not supplied' and silently auto-incremented. The bash twin keys off whether the value is non-empty (`[ -z "$BRANCH_NUMBER" ]`), so `--number 0` is honored and yields FEATURE_NUM 000 -- a cross-platform divergence for identical input. Use $PSBoundParameters.ContainsKey('Number') instead, so an explicit value (including 0) is honored and only a missing -Number auto-detects -- mirroring bash. This also aligns the -Timestamp+-Number warning, which bash emits for `--number 0 --timestamp` (non-empty check) but PowerShell previously skipped. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent b7e67f5 commit 3b2ba61

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

β€Žscripts/powershell/create-new-feature.ps1β€Ž

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ if ($ShortName) {
142142
$branchSuffix = Get-BranchName -Description $featureDesc
143143
}
144144

145-
# Warn if -Number and -Timestamp are both specified
146-
if ($Timestamp -and $Number -ne 0) {
145+
# Warn if -Number and -Timestamp are both specified. Use ContainsKey (not
146+
# `-ne 0`) so an explicit `-Number 0` is also detected, matching the bash twin's
147+
# `[ -n "$BRANCH_NUMBER" ]` check.
148+
if ($Timestamp -and $PSBoundParameters.ContainsKey('Number')) {
147149
Write-Warning "[specify] Warning: -Number is ignored when -Timestamp is used"
148150
$Number = 0
149151
}
@@ -153,8 +155,10 @@ if ($Timestamp) {
153155
$featureNum = Get-Date -Format 'yyyyMMdd-HHmmss'
154156
$branchName = "$featureNum-$branchSuffix"
155157
} else {
156-
# Determine branch number from existing feature directories
157-
if ($Number -eq 0) {
158+
# Determine branch number from existing feature directories. Auto-detect only
159+
# when -Number was not supplied; an explicit value (including 0) is honored,
160+
# matching the bash twin's `[ -z "$BRANCH_NUMBER" ]` check.
161+
if (-not $PSBoundParameters.ContainsKey('Number')) {
158162
$Number = (Get-HighestNumberFromSpecs -SpecsDir $specsDir) + 1
159163
}
160164

β€Žtests/test_timestamp_branches.pyβ€Ž

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,19 @@ def test_sequential_supports_four_digit_prefixes(self, git_repo: Path):
275275
branch = line.split(":", 1)[1].strip()
276276
assert branch == "1001-next-feat", f"expected 1001-next-feat, got: {branch}"
277277

278+
def test_explicit_number_zero_is_honored(self, git_repo: Path):
279+
"""An explicit --number 0 is honored literally (FEATURE_NUM 000), not treated
280+
as auto-detect, even when higher-numbered specs already exist. This pins the
281+
canonical bash behavior the PowerShell twin must mirror."""
282+
(git_repo / "specs" / "003-existing").mkdir(parents=True)
283+
r = run_script(
284+
git_repo, "--json", "--dry-run", "--number", "0", "--short-name", "zero", "Zero feature",
285+
)
286+
assert r.returncode == 0, r.stderr
287+
data = json.loads(r.stdout)
288+
assert data["FEATURE_NUM"] == "000"
289+
assert data["BRANCH_NAME"] == "000-zero"
290+
278291

279292
class TestSequentialBranchPowerShell:
280293
def test_powershell_scanner_uses_long_tryparse_for_large_prefixes(self):
@@ -302,6 +315,23 @@ def _run(desc: str) -> subprocess.CompletedProcess:
302315
assert r2.returncode == 0, r2.stderr
303316
assert json.loads(r2.stdout)["BRANCH_NAME"] == "001-use-go-now"
304317

318+
@pytest.mark.skipif(not _has_pwsh(), reason="pwsh not installed")
319+
def test_explicit_number_zero_is_honored_matching_bash(self, ps_git_repo: Path):
320+
"""An explicit -Number 0 must be honored (FEATURE_NUM 000) like the bash twin,
321+
even when higher-numbered specs exist. Before the fix, PowerShell could not
322+
distinguish -Number 0 from the default and silently auto-detected (e.g. 004)."""
323+
script = ps_git_repo / "scripts" / "powershell" / "create-new-feature.ps1"
324+
(ps_git_repo / "specs" / "003-existing").mkdir(parents=True)
325+
result = subprocess.run(
326+
["pwsh", "-NoProfile", "-File", str(script),
327+
"-Json", "-DryRun", "-Number", "0", "-ShortName", "zero", "Zero feature"],
328+
cwd=ps_git_repo, capture_output=True, text=True,
329+
)
330+
assert result.returncode == 0, result.stderr
331+
data = json.loads(result.stdout)
332+
assert data["FEATURE_NUM"] == "000"
333+
assert data["BRANCH_NAME"] == "000-zero"
334+
305335

306336
# ── check_feature_branch Tests ───────────────────────────────────────────────
307337

0 commit comments

Comments
Β (0)