Skip to content

Commit 134bc5e

Browse files
Improve pre-push checks and correct historic mistakes []
This change introduces an improved version of the pre-push checker that: - allows the spell checking functions to work better with CamelCase names, - adds a per-directory dictionary to allow domain-specific exceptions, - excludes binary files from checks, - understands that md tables, rst blocks, and rst tables cannot be wrapped, - and does not enforce line length on yml, yaml, or json files. Additionally, as the prior file state is assumed to contain no spelling mistakes, a pass has been taken to correct spelling mistakes so further mistakes of the same kind are caught.
1 parent 0c7d6db commit 134bc5e

42 files changed

Lines changed: 1838 additions & 115 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
name: edg-spelling-allowlist
3+
description: >-
4+
How the pre-push spell check (edg-check-file-changes, aspell, edgspelling)
5+
decides what to report, and how to add words to the hierarchical spelling
6+
allowlist (dev_tools/spelling_allowlist.json). Use when the pre-push hook
7+
or edg-collect-changes --check reports misspelled words, when choosing
8+
where a new allowed word belongs, or when editing the allowlist file.
9+
---
10+
11+
# Spell check and the spelling allowlist
12+
13+
## How the check works
14+
15+
The pre-push hook (`.git-hooks/pre-push.py`) runs
16+
`dev_tools/bin/edg-check-file-changes` on every changed file, through
17+
`edg-exec` when docker is preferred. Its spelling pass:
18+
19+
- Runs `aspell list` on the text. For code files (`.c`, `.cpp`, `.h`,
20+
`.hpp`) only the comments are checked (extracted with `get-comments`).
21+
Other files are checked whole.
22+
- First splits identifiers into words (`edgspelling.preprocess`): `fooBar`
23+
becomes `foo Bar`. Underscores already split words, so `foo_bar` is
24+
checked as `foo` and `bar`.
25+
- Reports only words that are **new** relative to the old version of the
26+
file, minus the allowlist words for that file's path.
27+
- For pending Changes entries (`*/pending_changes/*.txt`), also accepts
28+
the entry's `//allowed_words:` directives and any word already in the
29+
target `Changes` file (see skill **edg-changes-example**). CI runs the
30+
same check with `edg-collect-changes --check`.
31+
32+
The hook skips some paths entirely (`EXCLUDES_PATH_PREFIXES` in
33+
`.git-hooks/pre-push.py`), including `AGENTS.md`, `.agents/`, and the
34+
allowlist itself.
35+
36+
## The allowlist file
37+
38+
`dev_tools/spelling_allowlist.json` is a tree of nodes. Each node may have:
39+
40+
| Key | Meaning |
41+
| --- | --- |
42+
| `words` | Sorted lowercase words, allowed in any capitalization |
43+
| `names` | Sorted words allowed only with exactly this capitalization |
44+
| `paths` | Object mapping one path component (directory or file name) to a child node |
45+
46+
A file's allowed words are the union of the nodes on the way down to it, so a
47+
word in the root node is allowed everywhere. The format is documented in
48+
`dev_tools/pylibs/edgspelling/__init__.py`.
49+
50+
Rules that `edgspelling.Allowlist` enforces when the file loads (a bad
51+
file makes the check refuse to run and fails `ShippedAllowlistTests` in CI):
52+
53+
- Each list is sorted by code point and has no duplicates.
54+
- `words` entries are lowercase. Use `names` when the capitalization must
55+
match (proper nouns like `EDG` or `Coverity`).
56+
- Only `words`, `names`, and `paths` keys are allowed.
57+
- Each entry is one word. `foo_bar` or `fooBar` would never match, because
58+
identifiers are split before lookup. Add the parts instead.
59+
60+
## Choosing where a word goes
61+
62+
Put a word in the **narrowest node** that covers the places it's used
63+
often (see "Prompting is the default" below):
64+
65+
- **Root:** terms used across the project (C++ terminology, `src`, `utf`).
66+
- **A directory node** (`src`, `dev_tools`, `dev_tools/pylibs/edgtest`,
67+
`doc/source`, …): jargon used only there. Common Python names like
68+
`isinstance` and `pathlib`, and the pylib package names (`edgutil`, …),
69+
live in `dev_tools`. The rare uses elsewhere (`dev-init.py`,
70+
`.git-hooks/`, `doc/source/conf.py`) are left to prompt.
71+
72+
A word used in only one file almost never earns an entry. The check
73+
reports only words that are new to a file, so the entry would save a prompt
74+
only when the word shows up in a second file. A word used only in a
75+
`Changes` file never needs one, because pending entries already accept
76+
every word in their target `Changes` file.
77+
- **A file node:** a one-off term in a single file.
78+
79+
### Prompting is the default
80+
81+
Each allowed word is a blind spot: a typo that happens to match it is never
82+
reported anywhere the entry applies. A broader node makes the blind spot
83+
bigger. Being prompted again costs little, because the check reports only
84+
words that are new to a file. Weigh the two:
85+
86+
- Prefer leaving a word out and letting people be prompted. Add it only when
87+
people would otherwise be prompted for it very often (for example, a term
88+
used constantly across a directory or the project).
89+
- When a word does earn an entry, choose the narrowest node that stops the
90+
frequent prompts, even if rare uses elsewhere still prompt. For example,
91+
the Python names are in `dev_tools` rather than at the root.
92+
- Be most careful with short words and words that are one letter away from
93+
a common English word (`len`, `str`, `dir`). They're the likeliest to hide
94+
a real typo.
95+
96+
Don't add a real misspelling. Fix the text. For a Changes entry, prefer an
97+
`//allowed_words:` directive over a new allowlist word unless the term will
98+
recur.
99+
100+
## Editing the file
101+
102+
Keep the existing formatting (one-space JSON indent, UTF-8, non-ASCII kept as
103+
is). A Python round trip keeps it exact and keeps the lists sorted:
104+
105+
```python
106+
import json, pathlib
107+
path = pathlib.Path('dev_tools/spelling_allowlist.json')
108+
tree = json.loads(path.read_text(encoding = 'utf-8'))
109+
tree['words'] = sorted(set(tree['words']) | {'newword'})
110+
# For a nested node: tree['paths']['src']['words'] = ...
111+
path.write_text(
112+
json.dumps(tree, indent = 1, ensure_ascii = False) + '\n',
113+
encoding = 'utf-8'
114+
)
115+
```
116+
117+
Then check that the file still loads, and run the tests:
118+
119+
```sh
120+
PYTHONPATH=dev_tools/pylibs python3 -c 'import edgspelling; edgspelling.Allowlist()'
121+
PYTHONPATH=dev_tools/pylibs python3 -m unittest discover -s dev_tools/tests \
122+
-p 'test_edg_check_file_changes.py'
123+
```
124+
125+
To see what aspell rejects in some text:
126+
`printf '%s\n' word1 word2 | aspell list`.

‎.git-hooks/pre-push.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
)
5151

5252
import edgchanges
53+
import edgshell
5354
import edgutil
5455

5556
if platform.system() == 'Windows':
@@ -94,7 +95,9 @@
9495
'src/error_msg.txt',
9596
# Don't check AGENT files.
9697
'AGENTS.md',
97-
'.agents/'
98+
'.agents/',
99+
# Don't check the spelling allowlist; it is made of words aspell rejects.
100+
'dev_tools/spelling_allowlist.json'
98101
]
99102

100103
def _is_excluded_file_path(path_str: str) -> bool:
@@ -234,6 +237,12 @@ def check_changed_files(self, first_new_commit):
234237
['diff', '--no-commit-id', '--name-only', '-r', f"{remote_head}..HEAD"]
235238
).splitlines()
236239

240+
# Binary files have no text to check, so don't extract them. Ask git
241+
# rather than sniffing the contents so that .gitattributes is honored.
242+
binary_files = edgshell.list_binary_files_in_git_diff(
243+
f"{remote_head}..HEAD"
244+
)
245+
237246
mono_repo_dir = edgutil.find_mono_repo_or_exit()
238247
containing_tmp_dir = mono_repo_dir / '.tmp'
239248

@@ -242,6 +251,8 @@ def check_changed_files(self, first_new_commit):
242251
for file_path_str in changed_files:
243252
if _is_excluded_file_path(file_path_str):
244253
continue
254+
if file_path_str in binary_files:
255+
continue
245256

246257
file_path = Path(file_path_str)
247258
named_tmp_args = {

‎.github/workflows/changes-fragments.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ on:
2222
- dev_tools/bin/edg-check-file-changes
2323
- dev_tools/bin/edg-collect-changes
2424
- dev_tools/pylibs/edgchanges/**
25+
- dev_tools/pylibs/edgspelling/**
2526
- dev_tools/pylibs/edgutil/**
27+
- dev_tools/spelling_allowlist.json
2628
pull_request:
2729
paths:
2830
- '**/pending_changes/**'
2931
- .github/workflows/changes-fragments.yml
3032
- dev_tools/bin/edg-check-file-changes
3133
- dev_tools/bin/edg-collect-changes
3234
- dev_tools/pylibs/edgchanges/**
35+
- dev_tools/pylibs/edgspelling/**
3336
- dev_tools/pylibs/edgutil/**
37+
- dev_tools/spelling_allowlist.json
3438

3539
permissions:
3640
contents: read

‎.github/workflows/dev-tools-tests.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ on:
1616
- dev_tools/bin/edg-check-file-changes
1717
- dev_tools/bin/edg-collect-changes
1818
- dev_tools/pylibs/edgchanges/**
19+
- dev_tools/pylibs/edgshell/**
20+
- dev_tools/pylibs/edgspelling/**
1921
- dev_tools/pylibs/edgutil/**
22+
- dev_tools/spelling_allowlist.json
2023
- dev_tools/tests/**
2124
merge_group:
2225

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,6 @@ If an agent does not auto-load skills, open the matching
149149
| `edg-cpfe-style` | [`.agents/skills/edg-cpfe-style/SKILL.md`](.agents/skills/edg-cpfe-style/SKILL.md) | Front-end coding style (`doc/source/code_style.rst`) |
150150
| `edg-python-style` | [`.agents/skills/edg-python-style/SKILL.md`](.agents/skills/edg-python-style/SKILL.md) | Python under `dev_tools/` (no formal RST; match in-tree) |
151151
| `edg-changes-example` | [`.agents/skills/edg-changes-example/SKILL.md`](.agents/skills/edg-changes-example/SKILL.md) | Adding a Changes entry (`pending_changes/`); encoding non-UTF-8 examples in any `Changes` file |
152+
| `edg-spelling-allowlist` | [`.agents/skills/edg-spelling-allowlist/SKILL.md`](.agents/skills/edg-spelling-allowlist/SKILL.md) | Spell check reports from pre-push or `edg-collect-changes --check`; adding words to `dev_tools/spelling_allowlist.json` |
152153
| `acknowledg-client` | [`.agents/skills/acknowledg-client/SKILL.md`](.agents/skills/acknowledg-client/SKILL.md) | Batch/Direct client, reconnect, provider CLIs |
153154
| `acknowledg-bench` | [`.agents/skills/acknowledg-bench/SKILL.md`](.agents/skills/acknowledg-bench/SKILL.md) | `.cgout` / Ir / baselines / bench review |

0 commit comments

Comments
 (0)