Skip to content

Commit e76520a

Browse files
fix(extensions/git): reject negative -Number in create-new-feature-branch.ps1
The bash and Python twins validate --number against ^[0-9]+$ and reject a negative value with 'Error: --number must be a non-negative integer'. The PowerShell twin declares the parameter as [long]$Number, so PowerShell binds '-5' as -5 instead of rejecting it. That value then formats via '{0:000}' to '-005' and yields a branch name starting with a dash, which git refuses (refs cannot begin with '-') β€” a confusing late failure instead of the twins' clear early error. Guard for $Number -lt 0 up front (before the description check, matching the bash twin's parse-time validation order) and emit the identical error. An explicit -Number 0 is still honored, preserving the #3412 fix. Add matching negative-number parity tests to the bash and PowerShell create-feature suites, mirroring the existing test_explicit_number_zero_is_honored pair. Same PowerShell-parity bug class as #3412. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent faeb956 commit e76520a

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ if ($Help) {
4141
exit 0
4242
}
4343

44+
# -Number is [long], so PowerShell binds "-5" as -5 rather than rejecting it
45+
# the way the bash/Python twins do (`^[0-9]+$`). A negative value would format
46+
# via '{0:000}' to e.g. "-005" and produce a branch name starting with "-",
47+
# which git refuses (refs cannot begin with a dash). Reject it here, before the
48+
# description check, matching the bash twin's parse-time validation order.
49+
if ($Number -lt 0) {
50+
Write-Error 'Error: --number must be a non-negative integer'
51+
exit 1
52+
}
53+
4454
if (-not $FeatureDescription -or $FeatureDescription.Count -eq 0) {
4555
Write-Error "Usage: ./create-new-feature-branch.ps1 [-Json] [-DryRun] [-AllowExistingBranch] [-ShortName <name>] [-Number N] [-Timestamp] <feature description>"
4656
exit 1

β€Žtests/extensions/git/test_git_extension.pyβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,19 @@ def test_explicit_number_zero_is_honored(self, tmp_path: Path):
653653
assert data["BRANCH_NAME"] == "000-zero"
654654
assert data["FEATURE_NUM"] == "000"
655655

656+
def test_negative_number_rejected(self, tmp_path: Path):
657+
"""A negative --number is rejected. Pins the canonical behavior the
658+
PowerShell twin must mirror; a negative value would otherwise format to
659+
e.g. '-005' and produce a branch name starting with '-', which git
660+
refuses (refs cannot begin with a dash)."""
661+
project = _setup_project(tmp_path)
662+
result = _run_bash(
663+
"create-new-feature-branch.sh", project,
664+
"--json", "--dry-run", "--number", "-5", "--short-name", "neg", "Negative feature",
665+
)
666+
assert result.returncode != 0
667+
assert "--number must be a non-negative integer" in result.stderr
668+
656669

657670
@pytest.mark.skipif(not HAS_PWSH, reason="pwsh not available")
658671
class TestCreateFeaturePowerShell:
@@ -974,6 +987,21 @@ def test_explicit_number_zero_is_honored(self, tmp_path: Path):
974987
assert data["BRANCH_NAME"] == "000-zero"
975988
assert data["FEATURE_NUM"] == "000"
976989

990+
def test_negative_number_rejected(self, tmp_path: Path):
991+
"""A negative -Number is rejected, matching the bash/Python twins'
992+
'--number must be a non-negative integer'. Regression guard: -Number is
993+
[long], so PowerShell binds '-5' as -5 rather than rejecting it the way
994+
the twins' `^[0-9]+$` check does; the value would then format via
995+
'{0:000}' to '-005' and yield a branch name starting with '-', which
996+
git refuses (refs cannot begin with a dash)."""
997+
project = _setup_project(tmp_path)
998+
result = _run_pwsh(
999+
"create-new-feature-branch.ps1", project,
1000+
"-Json", "-DryRun", "-Number", "-5", "-ShortName", "neg", "Negative feature",
1001+
)
1002+
assert result.returncode != 0
1003+
assert "--number must be a non-negative integer" in result.stderr
1004+
9771005

9781006
# ── auto-commit.sh Tests ─────────────────────────────────────────────────────
9791007

0 commit comments

Comments
Β (0)