Skip to content

💫 Toolkit to help you get started with SDD or any other process!

fix(scripts): make bash branch-name sanitizing match the Python and PowerShell twins (#4286)

Sepuri Sai Krishna18ea13fdc51a
diff --git a/extensions/git/scripts/bash/create-new-feature-branch.sh b/extensions/git/scripts/bash/create-new-feature-branch.sh
index 856cb0bec..5303bb97c 100755
--- a/extensions/git/scripts/bash/create-new-feature-branch.sh
+++ b/extensions/git/scripts/bash/create-new-feature-branch.sh
@@ -206,9 +206,18 @@ check_existing_branches() {
}
# Function to clean and format a branch name
+#
+# Three details keep this byte-identical to the Python and PowerShell twins:
+# * LC_ALL=C -- in a UTF-8 locale glibc resolves the a-z *range* through
+# collation, so [^a-z0-9] keeps accented lowercase letters that
+# re.sub(r"[^a-z0-9]", ...) and .NET's -replace both strip.
+# * `--*` instead of the GNU-only `\+`, which POSIX/BSD sed reads as a literal
+# '+', leaving repeated separators uncollapsed on macOS.
+# * printf instead of echo, so a name of "-n"/"-e"/"-E" is text, not options.
clean_branch_name() {
local name="$1"
- echo "$name" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/-\+/-/g' | sed 's/^-//' | sed 's/-$//'
+ local -x LC_ALL=C
+ printf '%s\n' "$name" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/--*/-/g' | sed 's/^-//' | sed 's/-$//'
}
# ---------------------------------------------------------------------------
@@ -444,6 +453,10 @@ generate_branch_name() {
local stop_words="^(i|a|an|the|to|for|of|in|on|at|by|with|from|is|are|was|were|be|been|being|have|has|had|do|does|did|will|would|should|could|can|may|might|must|shall|this|that|these|those|my|your|our|their|want|need|add|get|set)$"
+ # LC_ALL=C for the same collation reason documented on clean_branch_name,
+ # and so the `grep -qw` acronym probe below uses ASCII word boundaries like
+ # the Python twin's (?<![0-9A-Za-z_]) lookarounds.
+ local -x LC_ALL=C
local clean_name=$(printf '%s' "$description" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/ /g')
local meaningful_words=()
diff --git a/extensions/git/scripts/powershell/create-new-feature-branch.ps1 b/extensions/git/scripts/powershell/create-new-feature-branch.ps1
index 2d6f2bcfe..4d06a113a 100644
--- a/extensions/git/scripts/powershell/create-new-feature-branch.ps1
+++ b/extensions/git/scripts/powershell/create-new-feature-branch.ps1
@@ -420,11 +420,13 @@ function Get-BranchName {
if ($stopWords -contains $word) { continue }
if ($word.Length -ge 3) {
$meaningfulWords += $word
- } elseif ($Description -cmatch "\b$($word.ToUpper())\b") {
+ } elseif ($Description -cmatch "(?<![0-9A-Za-z_])$($word.ToUpper())(?![0-9A-Za-z_])") {
# Case-sensitive (-cmatch) to mirror the bash twin's case-sensitive
# whole-word acronym match: keep a short word only when its UPPERCASE
# form appears in the original (an acronym). -match is case-insensitive
- # and would keep every short word.
+ # and would keep every short word. ASCII boundaries rather than \b,
+ # which is Unicode-aware in .NET and would miss an acronym sitting
+ # next to an accented letter that bash's LC_ALL=C grep still splits on.
$meaningfulWords += $word
}
}
diff --git a/extensions/git/scripts/python/create_new_feature_branch.py b/extensions/git/scripts/python/create_new_feature_branch.py
index e4b413515..202b1f796 100644
--- a/extensions/git/scripts/python/create_new_feature_branch.py
+++ b/extensions/git/scripts/python/create_new_feature_branch.py
@@ -297,8 +297,15 @@ def generate_branch_name(description: str) -> str:
if len(word) >= 3:
meaningful_words.append(word)
# Keep short words only when they appear uppercased in the original
- # description (acronyms like "API" or "DB").
- elif re.search(rf"\b{re.escape(word.upper())}\b", description):
+ # description (acronyms like "API" or "DB"). The boundaries are spelled
+ # out as ASCII rather than using \b: \b is Unicode-aware on str, so
+ # "\u00e9DB\u00e9 cache" would hide the acronym behind a non-ASCII word
+ # character, while the bash twin's `grep -qw` runs under LC_ALL=C and
+ # sees a boundary there.
+ elif re.search(
+ rf"(?<![0-9A-Za-z_]){re.escape(word.upper())}(?![0-9A-Za-z_])",
+ description,
+ ):
meaningful_words.append(word)
if meaningful_words:
diff --git a/scripts/bash/create-new-feature.sh b/scripts/bash/create-new-feature.sh
index abdb2194b..06681d9b6 100644
--- a/scripts/bash/create-new-feature.sh
+++ b/scripts/bash/create-new-feature.sh
@@ -145,9 +145,18 @@ spec_prefix_exists() {
}
# Function to clean and format a branch name
+#
+# Three details keep this byte-identical to the Python and PowerShell twins:
+# * LC_ALL=C -- in a UTF-8 locale glibc resolves the a-z *range* through
+# collation, so [^a-z0-9] keeps accented lowercase letters that
+# re.sub(r"[^a-z0-9]", ...) and .NET's -replace both strip.
+# * `--*` instead of the GNU-only `\+`, which POSIX/BSD sed reads as a literal
+# '+', leaving repeated separators uncollapsed on macOS.
+# * printf instead of echo, so a name of "-n"/"-e"/"-E" is text, not options.
clean_branch_name() {
local name="$1"
- echo "$name" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/-\+/-/g' | sed 's/^-//' | sed 's/-$//'
+ local -x LC_ALL=C
+ printf '%s\n' "$name" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/--*/-/g' | sed 's/^-//' | sed 's/-$//'
}
# Fit a feature prefix and suffix within GitHub's branch-name limit.
@@ -200,7 +209,11 @@ generate_branch_name() {
# Common stop words to filter out
local stop_words="^(i|a|an|the|to|for|of|in|on|at|by|with|from|is|are|was|were|be|been|being|have|has|had|do|does|did|will|would|should|could|can|may|might|must|shall|this|that|these|those|my|your|our|their|want|need|add|get|set)$"
- # Convert to lowercase and split into words
+ # Convert to lowercase and split into words. LC_ALL=C for the same
+ # collation reason documented on clean_branch_name, and so the `grep -qw`
+ # acronym probe below uses ASCII word boundaries like the Python twin's
+ # (?<![0-9A-Za-z_]) lookarounds.
+ local -x LC_ALL=C
local clean_name=$(printf '%s' "$description" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/ /g')
# Filter words: remove stop words and words shorter than 3 chars (unless they're uppercase acronyms in original)
diff --git a/scripts/powershell/create-new-feature.ps1 b/scripts/powershell/create-new-feature.ps1
index 216cca5e2..9ce2c678a 100644
--- a/scripts/powershell/create-new-feature.ps1
+++ b/scripts/powershell/create-new-feature.ps1
@@ -145,11 +145,13 @@ function Get-BranchName {
# Keep words that are length >= 3 OR appear as uppercase in original (likely acronyms)
if ($word.Length -ge 3) {
$meaningfulWords += $word
- } elseif ($Description -cmatch "\b$($word.ToUpper())\b") {
+ } elseif ($Description -cmatch "(?<![0-9A-Za-z_])$($word.ToUpper())(?![0-9A-Za-z_])") {
# Keep short words only if they appear as uppercase in original (likely
# acronyms). Use -cmatch so the comparison is case-sensitive, matching the
# bash script's case-sensitive grep; -match would be case-insensitive and
- # would keep every short word.
+ # would keep every short word. The boundaries are spelled out as ASCII
+ # because .NET's \b is Unicode-aware, so an accented letter next to the
+ # acronym would suppress the match that bash's LC_ALL=C grep still makes.
$meaningfulWords += $word
}
}
diff --git a/tests/extensions/git/test_git_extension_python_parity.py b/tests/extensions/git/test_git_extension_python_parity.py
index faeef00e7..8cae45e78 100644
--- a/tests/extensions/git/test_git_extension_python_parity.py
+++ b/tests/extensions/git/test_git_extension_python_parity.py
@@ -17,10 +17,13 @@ from pathlib import Path
import pytest
from tests.conftest import requires_bash
+from tests.parity_helpers import collation_range_locale
from tests.extensions.git.test_git_extension import (
_GIT_ENV,
+ HAS_PWSH,
_init_git,
_run_bash,
+ _run_pwsh,
_setup_project,
_write_config,
)
@@ -139,20 +142,144 @@ class TestCreateFeatureBranchParity:
p = _run_py("create-new-feature-branch", py_proj, "--json", "--dry-run", description)
_assert_parity(b, p)
- def test_short_name_cleaning(self, tmp_path: Path):
+ @pytest.mark.parametrize(
+ "description",
+ [
+ "Fix \u00e9DB\u00e9 sync",
+ "Tune the \u00fcUI\u00fc layer",
+ ],
+ ids=["db_between_accents", "ui_between_accents"],
+ )
+ def test_acronym_adjacent_to_non_ascii_matches_python(
+ self, tmp_path: Path, description: str
+ ):
+ """A short acronym touching an accented letter is kept by both twins.
+
+ The bash twin probes for acronyms with `grep -qw` under LC_ALL=C, where
+ an accented letter is a non-word byte and therefore a word boundary.
+ Python's \\b and .NET's \\b are Unicode-aware and saw no boundary there,
+ so the twins disagreed on whether the acronym survived.
+ """
+ bash_proj, py_proj = _twin_projects(tmp_path)
+ b = _run_bash(
+ "create-new-feature-branch.sh", bash_proj,
+ "--json", "--dry-run", description,
+ )
+ p = _run_py(
+ "create-new-feature-branch", py_proj,
+ "--json", "--dry-run", description,
+ )
+ _assert_parity(b, p)
+
+ if not HAS_PWSH:
+ pytest.skip("pwsh not available")
+ ps_proj = _setup_project(tmp_path / "ps" / "proj")
+ ps = _run_pwsh(
+ "create-new-feature-branch.ps1", ps_proj,
+ "-Json", "-DryRun", description,
+ )
+ assert ps.returncode == 0, ps.stderr
+ assert json.loads(ps.stdout) == json.loads(b.stdout)
+
+ @pytest.mark.parametrize(
+ ("short_name", "expected"),
+ [
+ ("User_Auth!", "001-user-auth"),
+ ("User__Auth!!", "001-user-auth"),
+ ("auth -- v2", "001-auth-v2"),
+ ],
+ ids=["single_separators", "repeated_separators", "separator_run"],
+ )
+ def test_short_name_cleaning(
+ self, tmp_path: Path, short_name: str, expected: str
+ ):
+ # Repeated separators included: the bash twin used to collapse them with
+ # sed 's/-\+/-/g', a GNU-ism that POSIX/BSD sed reads as a literal '+',
+ # so the runs survived on macOS.
bash_proj, py_proj = _twin_projects(tmp_path)
- # Single separator runs only: the bash twin's collapse step
- # (sed 's/-\+/-/g') is a GNU-ism that BSD sed treats literally.
b = _run_bash(
"create-new-feature-branch.sh", bash_proj,
- "--json", "--dry-run", "--short-name", "User_Auth!", "desc",
+ "--json", "--dry-run", "--short-name", short_name, "desc",
)
p = _run_py(
"create-new-feature-branch", py_proj,
- "--json", "--dry-run", "--short-name", "User_Auth!", "desc",
+ "--json", "--dry-run", "--short-name", short_name, "desc",
)
_assert_parity(b, p)
- assert json.loads(p.stdout)["BRANCH_NAME"] == "001-user-auth"
+ assert json.loads(p.stdout)["BRANCH_NAME"] == expected
+
+ @pytest.mark.parametrize(
+ "description",
+ [
+ "Añadir autenticación de usuario",
+ "Ajouter la réservation hôtelière",
+ ],
+ ids=["spanish", "french"],
+ )
+ def test_branch_name_ignores_locale_collation(
+ self, tmp_path: Path, description: str
+ ):
+ """The created git branch must not depend on the caller's locale.
+
+ The bash twin sanitizes with sed 's/[^a-z0-9]/-/g'. Under a locale whose
+ a-z range is collation-ordered that class keeps accented lowercase
+ letters, so bash checked out 001-ajouter-réservation-hôtelière where
+ the Python twin checks out 001-ajouter-servation-teli.
+ """
+ locale_name = collation_range_locale()
+ if locale_name is None:
+ pytest.skip("no locale with collation-ordered [a-z] ranges available")
+ env_extra = {"LC_ALL": locale_name, "LANG": locale_name}
+
+ bash_proj, py_proj = _twin_projects(tmp_path)
+ b = _run_bash(
+ "create-new-feature-branch.sh", bash_proj,
+ "--json", "--dry-run", description, env_extra=env_extra,
+ )
+ p = _run_py(
+ "create-new-feature-branch", py_proj,
+ "--json", "--dry-run", description, env_extra=env_extra,
+ )
+ _assert_parity(b, p)
+ branch = json.loads(b.stdout)["BRANCH_NAME"]
+ assert branch.isascii(), branch
+
+ # The run above reaches generate_branch_name. --short-name reaches
+ # clean_branch_name, a separate function carrying its own LC_ALL=C, so
+ # exercise the accented value through both: neither copy can then
+ # regress on its own without a failure here.
+ sn_bash_proj, sn_py_proj = _twin_projects(tmp_path / "short")
+ sb = _run_bash(
+ "create-new-feature-branch.sh", sn_bash_proj,
+ "--json", "--dry-run", "--short-name", description, "desc",
+ env_extra=env_extra,
+ )
+ sp = _run_py(
+ "create-new-feature-branch", sn_py_proj,
+ "--json", "--dry-run", "--short-name", description, "desc",
+ env_extra=env_extra,
+ )
+ _assert_parity(sb, sp)
+ short_branch = json.loads(sb.stdout)["BRANCH_NAME"]
+ assert short_branch.isascii(), short_branch
+
+ @pytest.mark.parametrize("short_name", ["-n", "-e", "-E"], ids=["n", "e", "E"])
+ def test_dash_prefixed_short_name(self, tmp_path: Path, short_name: str):
+ """A short name that looks like an ``echo`` option is still text.
+
+ clean_branch_name piped the raw value through `echo "$name"`, so bash
+ consumed -n/-e/-E as options and emitted nothing, yielding a suffix-less
+ 001- where Python yields 001-n. This extension duplicates the
+ implementation, so it needs its own regression case: the core script's
+ test cannot catch a revert to `echo` here.
+ """
+ bash_proj, py_proj = _twin_projects(tmp_path)
+ args = ("--json", "--dry-run", "--short-name", short_name, "desc")
+ b = _run_bash("create-new-feature-branch.sh", bash_proj, *args)
+ p = _run_py("create-new-feature-branch", py_proj, *args)
+ _assert_parity(b, p)
+ expected = f"001-{short_name.lstrip('-').lower()}"
+ assert json.loads(b.stdout)["BRANCH_NAME"] == expected
def test_numbering_from_specs_and_branches(self, tmp_path: Path):
bash_proj, py_proj = _twin_projects(tmp_path)
diff --git a/tests/parity_helpers.py b/tests/parity_helpers.py
index 3a3878de8..5ab3fcfa0 100644
--- a/tests/parity_helpers.py
+++ b/tests/parity_helpers.py
@@ -82,6 +82,37 @@ def clean_env() -> dict[str, str]:
return env
+def collation_range_locale() -> str | None:
+ """A locale whose ``[a-z]`` bracket range is collation-ordered, or ``None``.
+
+ glibc resolves a bracket-expression *range* through the locale's collation
+ table, so under ``en_US.UTF-8`` ``[^a-z0-9]`` leaves accented lowercase
+ letters alone while ``C.UTF-8`` and the POSIX locale strip them. Probe
+ ``sed`` directly rather than trusting a locale name: the environments where
+ the divergence cannot be reproduced (no such locale installed, a non-glibc
+ libc, Git-for-Windows) are exactly the ones where the probe comes back
+ clean, so the caller can skip.
+ """
+ for name in ("en_US.UTF-8", "en_US.utf8"):
+ env = clean_env()
+ env["LC_ALL"] = name
+ env["LANG"] = name
+ try:
+ probe = subprocess.run(
+ ["sed", "s/[^a-z0-9]/-/g"],
+ input="é\n",
+ capture_output=True,
+ text=True,
+ check=False,
+ env=env,
+ )
+ except OSError: # pragma: no cover - sed missing entirely
+ return None
+ if probe.returncode == 0 and "é" in probe.stdout:
+ return name
+ return None
+
+
def run(
cmd: list[str],
repo: Path,
diff --git a/tests/test_create_new_feature_python_parity.py b/tests/test_create_new_feature_python_parity.py
index 74d071ad5..6cc50d80e 100644
--- a/tests/test_create_new_feature_python_parity.py
+++ b/tests/test_create_new_feature_python_parity.py
@@ -14,6 +14,8 @@ from tests.parity_helpers import (
HAS_POWERSHELL,
bash_cmd,
break_wrap_layer,
+ clean_env,
+ collation_range_locale,
install_composition_stack,
install_scripts,
json_stdout,
@@ -117,8 +119,18 @@ def test_python_prefix_scan_tolerates_permission_error(
"I want to add the new API rate limiting feature for users",
"Fix UI for DB sync",
"a to the of",
+ # An acronym touching an accented letter: bash probes with `grep -qw`
+ # under LC_ALL=C, where the accent is a word boundary, so the Python
+ # twin must use explicit ASCII lookarounds rather than a Unicode \b.
+ "Fix \u00e9DB\u00e9 sync",
+ ],
+ ids=[
+ "plain",
+ "stop_words",
+ "acronyms",
+ "all_stop_words_fallback",
+ "acronym_next_to_non_ascii",
],
- ids=["plain", "stop_words", "acronyms", "all_stop_words_fallback"],
)
def test_python_branch_name_generation_matches_bash(
repo: Path, description: str
@@ -131,6 +143,26 @@ def test_python_branch_name_generation_matches_bash(
assert json_stdout(py) == json_stdout(bash)
+@requires_bash
[email protected](not HAS_POWERSHELL, reason="no PowerShell available")
+def test_all_variants_keep_acronym_next_to_non_ascii(repo: Path) -> None:
+ """An acronym touching an accented letter survives in all three twins.
+
+ bash probes for acronyms with `grep -qw` under LC_ALL=C, where an accented
+ letter is a non-word byte and therefore a boundary. Python's \\b and .NET's
+ \\b are Unicode-aware and saw "\u00e9DB\u00e9" as a single word, dropping the
+ acronym; all three now spell the boundary out as ASCII.
+ """
+ description = "Fix \u00e9DB\u00e9 sync"
+ bash = run(bash_cmd(repo, SCRIPT, "--json", "--dry-run", description), repo)
+ py = run(py_cmd(repo, SCRIPT, "--json", "--dry-run", description), repo)
+ ps = run(ps_cmd(repo, SCRIPT, "-Json", "-DryRun", description), repo)
+
+ assert bash.returncode == py.returncode == ps.returncode == 0
+ assert json_stdout(py) == json_stdout(bash) == json_stdout(ps)
+ assert json_stdout(ps)["BRANCH_NAME"] == "001-fix-db-sync"
+
+
@requires_bash
@pytest.mark.parametrize(
"args",
@@ -1067,6 +1099,111 @@ def test_all_variants_corrected_prefix_skips_timestamp_collision(repo: Path) ->
assert "using 20260320 instead" in result.stderr
+@requires_bash
+ "description",
+ [
+ "Añadir autenticación de usuario",
+ "Prüfung für Benutzer anlegen",
+ "Ajouter la réservation hôtelière",
+ ],
+ ids=["spanish", "german", "french"],
+)
+def test_bash_branch_name_ignores_locale_collation(
+ repo: Path, description: str
+) -> None:
+ """Branch naming must not depend on the caller's locale.
+
+ ``clean_branch_name``/``generate_branch_name`` sanitize with
+ ``sed 's/[^a-z0-9]/-/g'``. Run under a collation-ordered locale that class
+ keeps accented lowercase letters, so bash produced
+ ``001-ajouter-réservation-hôtelière`` where the Python and PowerShell twins
+ produce ``001-ajouter-servation-teli``: the same description yielded a
+ different ``specs/`` directory on two machines that differ only in ``LANG``.
+ """
+ locale_name = collation_range_locale()
+ if locale_name is None:
+ pytest.skip("no locale with collation-ordered [a-z] ranges available")
+
+ env = clean_env()
+ env["LC_ALL"] = locale_name
+ env["LANG"] = locale_name
+
+ bash = run(bash_cmd(repo, SCRIPT, "--json", "--dry-run", description), repo, env)
+ py = run(py_cmd(repo, SCRIPT, "--json", "--dry-run", description), repo, env)
+
+ assert py.returncode == bash.returncode == 0
+ assert json_stdout(py) == json_stdout(bash)
+ branch = json_stdout(bash)["BRANCH_NAME"]
+ assert isinstance(branch, str) and branch.isascii(), branch
+
+ # The run above reaches generate_branch_name. --short-name reaches
+ # clean_branch_name, a separate function carrying its own LC_ALL=C, so
+ # exercise the accented value through both: neither copy can then regress
+ # on its own without a failure here.
+ short_args = ("--json", "--dry-run", "--short-name", description, "x")
+ bash_short = run(bash_cmd(repo, SCRIPT, *short_args), repo, env)
+ py_short = run(py_cmd(repo, SCRIPT, *short_args), repo, env)
+
+ assert py_short.returncode == bash_short.returncode == 0
+ assert json_stdout(py_short) == json_stdout(bash_short)
+ short_branch = json_stdout(bash_short)["BRANCH_NAME"]
+ assert isinstance(short_branch, str) and short_branch.isascii(), short_branch
+
+
+@requires_bash
+ ("short_name", "expected"),
+ [
+ ("My Fancy!! Name", "001-my-fancy-name"),
+ ("auth -- v2", "001-auth-v2"),
+ ],
+ ids=["punctuation_run", "separator_run"],
+)
+def test_bash_collapses_repeated_separators(
+ repo: Path, short_name: str, expected: str
+) -> None:
+ """Runs of non-alphanumeric characters collapse to a single hyphen.
+
+ The bash twin squeezed them with ``sed 's/-\\+/-/g'``. ``\\+`` is a GNU
+ extension, not POSIX BRE: BSD ``sed`` (macOS) reads it as a literal ``+``,
+ so nothing collapsed and the branch became ``001-my-fancy---name``.
+ """
+ bash = run(
+ bash_cmd(repo, SCRIPT, "--json", "--dry-run", "--short-name", short_name, "x"),
+ repo,
+ )
+ py = run(
+ py_cmd(repo, SCRIPT, "--json", "--dry-run", "--short-name", short_name, "x"),
+ repo,
+ )
+
+ assert bash.returncode == py.returncode == 0
+ assert json_stdout(bash) == json_stdout(py)
+ assert json_stdout(bash)["BRANCH_NAME"] == expected
+
+
+@requires_bash
[email protected]("short_name", ["-n", "-e", "-E"], ids=["n", "e", "E"])
+def test_python_dash_prefixed_short_name_matches_bash(
+ repo: Path, short_name: str
+) -> None:
+ """A short name that looks like an ``echo`` option is still text.
+
+ ``clean_branch_name`` piped the raw value through ``echo "$name"``, so bash
+ consumed ``-n``/``-e``/``-E`` as options and emitted nothing, yielding the
+ suffix-less ``001-`` where Python yields ``001-n``.
+ """
+ args = ("--json", "--dry-run", "--short-name", short_name, "x")
+ bash = run(bash_cmd(repo, SCRIPT, *args), repo)
+ py = run(py_cmd(repo, SCRIPT, *args), repo)
+
+ assert py.returncode == bash.returncode == 0
+ assert json_stdout(py) == json_stdout(bash)
+ expected = f"001-{short_name.lstrip('-').lower()}"
+ assert json_stdout(bash)["BRANCH_NAME"] == expected
+
+
@pytest.mark.skipif(not HAS_POWERSHELL, reason="no PowerShell available")
@pytest.mark.parametrize(
"description",