fix(extensions/git): reject negative -Number in create-new-feature-branch.ps1 (#3538)

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>
This commit is contained in:
Noor ul ain
2026-07-15 22:48:24 +05:00
committed by GitHub
parent f065e27478
commit a7f6fe8dd4
2 changed files with 38 additions and 0 deletions

View File

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

View File

@@ -653,6 +653,19 @@ class TestCreateFeatureBash:
assert data["BRANCH_NAME"] == "000-zero"
assert data["FEATURE_NUM"] == "000"
def test_negative_number_rejected(self, tmp_path: Path):
"""A negative --number is rejected. Pins the canonical behavior the
PowerShell twin must mirror; a negative value would otherwise format to
e.g. '-005' and produce a branch name starting with '-', which git
refuses (refs cannot begin with a dash)."""
project = _setup_project(tmp_path)
result = _run_bash(
"create-new-feature-branch.sh", project,
"--json", "--dry-run", "--number", "-5", "--short-name", "neg", "Negative feature",
)
assert result.returncode != 0
assert "--number must be a non-negative integer" in result.stderr
@pytest.mark.skipif(not HAS_PWSH, reason="pwsh not available")
class TestCreateFeaturePowerShell:
@@ -974,6 +987,21 @@ class TestCreateFeaturePowerShell:
assert data["BRANCH_NAME"] == "000-zero"
assert data["FEATURE_NUM"] == "000"
def test_negative_number_rejected(self, tmp_path: Path):
"""A negative -Number is rejected, matching the bash/Python twins'
'--number must be a non-negative integer'. Regression guard: -Number is
[long], so PowerShell binds '-5' as -5 rather than rejecting it the way
the twins' `^[0-9]+$` check does; the value would then format via
'{0:000}' to '-005' and yield a branch name starting with '-', which
git refuses (refs cannot begin with a dash)."""
project = _setup_project(tmp_path)
result = _run_pwsh(
"create-new-feature-branch.ps1", project,
"-Json", "-DryRun", "-Number", "-5", "-ShortName", "neg", "Negative feature",
)
assert result.returncode != 0
assert "--number must be a non-negative integer" in result.stderr
# ── auto-commit.sh Tests ─────────────────────────────────────────────────────