-
Notifications
You must be signed in to change notification settings - Fork 0
/
codespellit
executable file
·206 lines (175 loc) · 5.6 KB
/
codespellit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/bash
#emacs: -*- mode: shell-script; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
#ex: set sts=4 ts=4 sw=4 et:
set -eu
source "$(readlink -f "$0" | xargs dirname)/common.sh"
codespell_version_full=$(codespell --version)
codespell_version=${codespell_version_full//.dev$/}
if [[ "$codespell_version_full" =~ \.dev ]]; then
# for the action we
codespell_dir=$(python -c 'import codespell_lib as l; print(l.__path__[0])')
codespell_version=$(
git -C "$codespell_dir" describe --tags --match 'v[0-9]*' | sed -e 's,-.*,,g' -e 's,^v,,g'
)
echo "WARNING! Running development version $codespell_version_full."
echo " For the action etc we will mention tagged version $codespell_version".
fi
function deduce-config() {
# deduce where we have that config
if [ -e ".codespellrc" ]; then
echo .codespellrc
elif grep -q '^\[tool.codespell\]' pyproject.toml 2>|/dev/null; then
echo pyproject.toml
elif grep -q '^\[codespell\]' setup.cfg 2>|/dev/null; then
echo setup.cfg
else
echo "ERROR: Cannot figure out where codespell config is" >&2
fi
}
function add-precommit() {
if [ "$#" -ge 1 ]; then
config="$1"
else
config=$(deduce-config)
fi
if [ -e .pre-commit-config.yaml ]; then
# add pre-commit configuration there
cat >> .pre-commit-config.yaml <<EOF
- repo: https://github.com/codespell-project/codespell
# Configuration for codespell is in $config
rev: v${codespell_version}
hooks:
- id: codespell
EOF
if [ "$config" = "pyproject.toml" ]; then
cat >> .pre-commit-config.yaml <<EOF
additional_dependencies:
- tomli
EOF
fi
git add .pre-commit-config.yaml
git commit -m 'Add pre-commit definition for codespell'
if ! python -c 'import yaml; yaml.safe_load(open(".pre-commit-config.yaml"))'; then
echo "ERROR: added pre-commit definition ruined the yaml format, fix and rerun '!0 list'"
exit 1
fi
else
echo "No .pre-commit-config.yaml was found, nothing changed"
fi
}
function list() {
codespell "$@" || {
echo
echo "Problematic?:"
echo
codespell "$@" | grep ','
}
}
function list-hits-sorted() {
codespell "$@" | grep -e '==>' | sed -e 's,.*: *,,g' | sort | uniq -c | sort -n
}
# given filtered output from list-hits-sorted, apply those which were left
function fix-sorted() { # shellcheck disable=SC2120
if ! git diff-index --quiet HEAD --; then
echo "Repository is dirty. Please commit your changes first."
exit 1
fi
nfixes=$(cat "$@" | grep -e '==>' -c )
cat "$@" | grep -e '==>' | while read -r _ fr _ to; do
git grep -l "\<$fr\>" | tr '\n' '\000' | xargs -0 sed -i -e "s,\<$fr\>,$to,g"
done
git commit -m "Fix $nfixes typos found by codespell
Here are typos with their counts and introduced fix:
$(grep -e '==>' "$@")
" -a
}
# poor man shortcuts for just doing it using the regex etc
cmd="${1:-}"
case "$cmd" in
list|list-hits-sorted|fix-sorted|deduce-config|add-precommit) shift; "$cmd" "$@"; exit $?;;
esac
git checkout -b enh-codespell
skips=".git*"
for f in '*.pdf' '*.svg' go.sum venv .venv venvs locale .tox versioneer.py package-lock.json vendor i18n '*-lock.yaml' '*.lock' '*.css' '*.min.*' '*-min.*' '*.pack.js' '*.niml' '*.gii'; do
if find . -iname "$f" -print -quit | grep -q .; then
skips+=",$f"
fi
done
ignore_regex=''
if find . -iname '*.ipynb' -print -quit | grep -q .; then
ignore_regex='^\s*"image/\S+": ".*'
fi
ignore_regex_prefix=''
if [ -z "$ignore_regex" ]; then
ignore_regex_prefix="# "
fi
if [ -e pyproject.toml ]; then
config="pyproject.toml"
cat >> pyproject.toml <<EOF
[tool.codespell]
# Ref: https://github.com/codespell-project/codespell#using-a-config-file
skip = '$skips'
check-hidden = true
${ignore_regex_prefix}ignore-regex = '$ignore_regex'
# ignore-words-list = ''
EOF
elif [ -e setup.cfg ]; then
config="setup.cfg"
# Then config file as the "last" commit we can amend if needed
cat >> setup.cfg <<EOF
[codespell]
# Ref: https://github.com/codespell-project/codespell#using-a-config-file
skip = $skips
check-hidden = true
${ignore_regex_prefix}ignore-regex = $ignore_regex
# ignore-words-list =
EOF
else
config=".codespellrc"
skips+=",$config" # for check-hidden
# Then config file as the "last" commit we can amend if needed
cat > .codespellrc <<EOF
[codespell]
# Ref: https://github.com/codespell-project/codespell#using-a-config-file
skip = $skips
check-hidden = true
${ignore_regex_prefix}ignore-regex = $ignore_regex
# ignore-words-list =
EOF
fi
if [ -e .github ]; then
mkdir -p .github/workflows
cat > .github/workflows/codespell.yml << EOF
# Codespell configuration is within $config
---
name: Codespell
on:
push:
branches: [$branch]
pull_request:
branches: [$branch]
permissions:
contents: read
jobs:
codespell:
name: Check for spelling errors
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Annotate locations with typos
uses: codespell-project/codespell-problem-matcher@v1
- name: Codespell
uses: codespell-project/actions-codespell@v2
EOF
git add .github/workflows/codespell.yml
git commit -m "Add github action to codespell $branch on push and PRs"
else
echo "No .github directory, no workflow provided"
fi
echo "Config in $config"
git add "$config"
# --no-verify since we might have trailing spaces which could trip some linters
git commit -m 'Add rudimentary codespell config' --no-verify "$config"
add-precommit "$config"
list "$@"