Skip to content

Commit b08ca8c

Browse files
authored
Lint changed Python files in CI and hooks (#63)
1 parent ff5c4c1 commit b08ca8c

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

.githooks/pre-commit

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ if [[ ! -x "$PYTHON_BIN" ]]; then
1414
exit 1
1515
fi
1616

17-
declare -a STAGED_NEW_FILES=()
17+
declare -a STAGED_CHANGED_FILES=()
1818
while IFS= read -r -d '' file; do
19-
STAGED_NEW_FILES+=("$file")
20-
done < <(git diff --cached --name-only --diff-filter=AR -z)
19+
STAGED_CHANGED_FILES+=("$file")
20+
done < <(git diff --cached --name-only --diff-filter=ACMR -z)
2121

2222
validate_leetcode_filenames() {
2323
local invalid=0
2424
local package_init='practice/leetcode/__init__.py'
2525
local solution_pattern='^practice/leetcode/solutions_[0-9]{5}/solution_[0-9]{5}\.py$'
2626
local init_pattern='^practice/leetcode/solutions_[0-9]{5}/__init__\.py$'
2727

28-
for file in ${STAGED_NEW_FILES[@]+"${STAGED_NEW_FILES[@]}"}; do
28+
for file in ${STAGED_CHANGED_FILES[@]+"${STAGED_CHANGED_FILES[@]}"}; do
2929
if [[ "$file" != practice/leetcode/* ]]; then
3030
continue
3131
fi
@@ -51,8 +51,8 @@ collect_new_python_files() {
5151
local file
5252
NEW_PYTHON_FILES=()
5353

54-
for file in ${STAGED_NEW_FILES[@]+"${STAGED_NEW_FILES[@]}"}; do
55-
if [[ "$file" == archive/* || "$file" == practice/leetcode/* ]]; then
54+
for file in ${STAGED_CHANGED_FILES[@]+"${STAGED_CHANGED_FILES[@]}"}; do
55+
if [[ "$file" == archive/* ]]; then
5656
continue
5757
fi
5858

@@ -71,9 +71,9 @@ declare -a NEW_PYTHON_FILES
7171
collect_new_python_files
7272

7373
if [[ ${#NEW_PYTHON_FILES[@]:-0} -eq 0 ]]; then
74-
echo "No newly added Python files staged for pylint."
74+
echo "No changed Python files staged for pylint."
7575
exit 0
7676
fi
7777

78-
echo "Running pylint on newly added Python files..."
78+
echo "Running pylint on changed Python files..."
7979
"$PYTHON_BIN" -m pylint --jobs=1 --fail-under="$PYLINT_FAIL_UNDER" "${NEW_PYTHON_FILES[@]}"

.github/workflows/pylint.yml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ jobs:
2626
run: |
2727
python -m pip install --upgrade pip
2828
pip install .[dev,reporter] pylint
29-
- name: Collect newly added Python files
30-
id: added-python
29+
- name: Collect changed Python files
30+
id: changed-python
3131
run: |
3232
if [ "${{ github.event_name }}" = "pull_request" ]; then
3333
base_sha="${{ github.event.pull_request.base.sha }}"
@@ -42,16 +42,15 @@ jobs:
4242
fi
4343
4444
python_files="$(
45-
git diff --name-only --diff-filter=AR "$base_sha" "$head_sha" -- '*.py' \
46-
':(exclude)archive/**' \
47-
':(exclude)practice/leetcode/**' | tr '\n' ' '
45+
git diff --name-only --diff-filter=ACMR "$base_sha" "$head_sha" -- '*.py' \
46+
':(exclude)archive/**' | tr '\n' ' '
4847
)"
4948
echo "files=$python_files" >> "$GITHUB_OUTPUT"
50-
- name: Analyse newly added Python files with pylint
51-
if: steps.added-python.outputs.files != ''
49+
- name: Analyse changed Python files with pylint
50+
if: steps.changed-python.outputs.files != ''
5251
run: |
53-
pylint --jobs=1 --fail-under="$PYLINT_FAIL_UNDER" ${{ steps.added-python.outputs.files }}
54-
- name: Skip pylint when no new Python files were added
55-
if: steps.added-python.outputs.files == ''
52+
pylint --jobs=1 --fail-under="$PYLINT_FAIL_UNDER" ${{ steps.changed-python.outputs.files }}
53+
- name: Skip pylint when no changed Python files were found
54+
if: steps.changed-python.outputs.files == ''
5655
run: |
57-
echo "No newly added Python files to lint."
56+
echo "No changed Python files to lint."

0 commit comments

Comments
 (0)