Skip to content

Commit f2c0f93

Browse files
mnriemCopilot
andcommitted
fix: classify Unicode consistently on Bash across locales
Use Python 3 for non-ASCII character classification where POSIX locale classes vary by platform. Preserve the shell-only ASCII path, report a clear error when a Unicode name lacks Python, and cover both paths. Refs #4574. Assisted-by: GitHub Copilot (model: GPT-6 Sol, autonomous) Co-authored-by: Copilot App <[email protected]>
1 parent 461d158 commit f2c0f93

3 files changed

Lines changed: 67 additions & 10 deletions

File tree

‎docs/reference/core.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ bash .specify/scripts/bash/create-new-feature.sh --json --short-name 用户管
7878

7979
The Python helper also accepts `--short-name`; the PowerShell helper uses
8080
`-ShortName`. A supplied short name is cleaned by the same rules, so it must
81-
contain at least one letter or digit. The Bash helper needs an installed UTF-8
82-
locale to recognize Unicode letters and digits. ASCII capitals are lowercased;
83-
non-ASCII letter casing is preserved across the script variants.
81+
contain at least one letter or digit. For non-ASCII names, the Bash helper needs
82+
an installed UTF-8 locale and a Python 3 interpreter for Unicode classification.
83+
ASCII capitals are lowercased; non-ASCII letter casing is preserved across the
84+
script variants.
8485

8586
## Check Installed Tools
8687

‎scripts/bash/create-new-feature.sh‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ spec_prefix_exists() {
147147
# Function to clean and format a branch name
148148
#
149149
# Three details keep this consistent with the Python and PowerShell twins:
150-
# * A UTF-8 locale makes [:alnum:] recognize Unicode letters and decimal digits.
150+
# * Unicode classification uses Python: POSIX [:alnum:] differs by platform.
151151
# * `--*` instead of the GNU-only `\+`, which POSIX/BSD sed reads as a literal
152152
# '+', leaving repeated separators uncollapsed on macOS.
153153
# * printf instead of echo, so a name of "-n"/"-e"/"-E" is text, not options.
154154
UNICODE_LOCALE=""
155155
for candidate in C.UTF-8 C.utf8 en_US.UTF-8 en_US.utf8 "${LC_ALL:-${LC_CTYPE:-${LANG:-}}}"; do
156-
if [ -n "$candidate" ] && [ "$(printf 'é٥²Ⅻ。' | LC_ALL="$candidate" sed 's/[^[:alnum:]]/-/g' 2>/dev/null)" = 'é٥---' ]; then
156+
if [ -n "$candidate" ] && [ "$(printf 'é。' | LC_ALL="$candidate" sed 's/[^[:alnum:]]/-/g' 2>/dev/null)" = 'é-' ]; then
157157
UNICODE_LOCALE="$candidate"
158158
break
159159
fi
@@ -167,10 +167,37 @@ if [ -z "$UNICODE_LOCALE" ]; then
167167
fi
168168
fi
169169

170+
unicode_words() {
171+
local name="$1"
172+
local separator="$2"
173+
if printf '%s' "$name" | LC_ALL=C grep -q '[^ -~]'; then
174+
local python_spec
175+
if ! python_spec=$(_python3_command); then
176+
echo "Error: Python 3 is required to create a Unicode feature name" >&2
177+
return 1
178+
fi
179+
local -a python_cmd
180+
read -r -a python_cmd <<< "$python_spec"
181+
printf '%s' "$name" | "${python_cmd[@]}" -c '
182+
import sys
183+
value = sys.stdin.buffer.read().decode("utf-8")
184+
lower = str.maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")
185+
result = "".join(
186+
char if char.isalpha() or char.isdecimal() else sys.argv[1]
187+
for char in value.translate(lower)
188+
)
189+
sys.stdout.buffer.write(result.encode("utf-8"))
190+
' "$separator"
191+
else
192+
printf '%s' "$name" | LC_ALL=C tr '[:upper:]' '[:lower:]' | LC_ALL=C sed "s/[^a-z0-9]/$separator/g"
193+
fi
194+
}
195+
170196
clean_branch_name() {
171197
local name="$1"
172-
local -x LC_ALL="$UNICODE_LOCALE"
173-
printf '%s\n' "$name" | LC_ALL=C tr '[:upper:]' '[:lower:]' | sed 's/[^[:alnum:]]/-/g' | sed 's/--*/-/g' | sed 's/^-//' | sed 's/-$//'
198+
local cleaned
199+
cleaned=$(unicode_words "$name" '-') || return 1
200+
printf '%s\n' "$cleaned" | sed 's/--*/-/g' | sed 's/^-//' | sed 's/-$//'
174201
}
175202

176203
branch_byte_count() {
@@ -232,10 +259,10 @@ generate_branch_name() {
232259
# Common stop words to filter out
233260
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)$"
234261

235-
# Use a UTF-8 locale for Unicode character classes. Lowercase only ASCII
236-
# so results do not depend on the platform's multibyte case conversion.
262+
# Use a UTF-8 locale for character-safe length checks and split words.
237263
local -x LC_ALL="$UNICODE_LOCALE"
238-
local clean_name=$(printf '%s\n' "$description" | LC_ALL=C tr '[:upper:]' '[:lower:]' | sed 's/[^[:alnum:]]/ /g')
264+
local clean_name
265+
clean_name=$(unicode_words "$description" ' ') || return 1
239266

240267
# Filter words: remove stop words and words shorter than 3 chars (unless they're uppercase acronyms in original)
241268
local meaningful_words=()

‎tests/test_create_new_feature_python_parity.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,35 @@ def test_bash_reports_missing_utf8_locale_for_unicode_only(
213213
assert not (repo / "specs").exists()
214214

215215

216+
@requires_bash
217+
def test_bash_requires_python_only_for_unicode_names(
218+
repo: Path, tmp_path: Path
219+
) -> None:
220+
shim_dir = tmp_path / "bin"
221+
shim_dir.mkdir()
222+
for name in ("python3", "python", "py"):
223+
shim = shim_dir / name
224+
shim.write_text("#!/bin/sh\nexit 1\n", encoding="utf-8", newline="\n")
225+
shim.chmod(0o755)
226+
env = clean_env()
227+
env["PATH"] = f"{shim_dir}:{env['PATH']}"
228+
229+
ascii_result = run(
230+
bash_cmd(repo, SCRIPT, "--json", "--dry-run", "Add user authentication"),
231+
repo,
232+
env,
233+
)
234+
assert ascii_result.returncode == 0, ascii_result.stderr
235+
assert json_stdout(ascii_result)["BRANCH_NAME"] == "001-user-authentication"
236+
237+
unicode_result = run(
238+
bash_cmd(repo, SCRIPT, "--json", "--dry-run", "添加用户"), repo, env
239+
)
240+
assert unicode_result.returncode == 1
241+
assert unicode_result.stdout == ""
242+
assert "Error: Python 3 is required to create a Unicode feature name" in unicode_result.stderr
243+
244+
216245
def _run_all_variants_allow_existing(
217246
repo: Path, *, number: str, short_name: str
218247
):

0 commit comments

Comments
 (0)