Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 654e239

Browse files
Add option to scripts-dev/lint.sh to only lint files changed since the last git commit (#8472)
This PR makes several changes to the `./scripts-dev/lint.sh` script, which lints the codebase with a number of tools: * Adds usage information, with `-h` flag to show it. Otherwise it will show when providing an unknown flag. * Adds option `-d` which will check both staged and unstaged files that have changed since the last commit and add them to the list of files to lint. - Note that only files without an extension, or with a `.py` extension will be allowed. This prevents editing bash scripts causing the linters to break on non-python files. * Improves the print-out of which files/directories are being linted.
1 parent 74976a8 commit 654e239

File tree

4 files changed

+90
-12
lines changed

4 files changed

+90
-12
lines changed

CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ run-time:
6363
./scripts-dev/lint.sh path/to/file1.py path/to/file2.py path/to/folder
6464
```
6565

66+
You can also provided the `-d` option, which will lint the files that have been
67+
changed since the last git commit. This will often be significantly faster than
68+
linting the whole codebase.
69+
6670
Before pushing new changes, ensure they don't produce linting errors. Commit any
6771
files that were corrected.
6872

changelog.d/8472.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `-d` option to `./scripts-dev/lint.sh` to lint files that have changed since the last git commit.

scripts-dev/lint.sh

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
#
33
# Runs linting scripts over the local Synapse checkout
44
# isort - sorts import statements
@@ -7,15 +7,90 @@
77

88
set -e
99

10-
if [ $# -ge 1 ]
11-
then
12-
files=$*
10+
usage() {
11+
echo
12+
echo "Usage: $0 [-h] [-d] [paths...]"
13+
echo
14+
echo "-d"
15+
echo " Lint files that have changed since the last git commit."
16+
echo
17+
echo " If paths are provided and this option is set, both provided paths and those"
18+
echo " that have changed since the last commit will be linted."
19+
echo
20+
echo " If no paths are provided and this option is not set, all files will be linted."
21+
echo
22+
echo " Note that paths with a file extension that is not '.py' will be excluded."
23+
echo "-h"
24+
echo " Display this help text."
25+
}
26+
27+
USING_DIFF=0
28+
files=()
29+
30+
while getopts ":dh" opt; do
31+
case $opt in
32+
d)
33+
USING_DIFF=1
34+
;;
35+
h)
36+
usage
37+
exit
38+
;;
39+
\?)
40+
echo "ERROR: Invalid option: -$OPTARG" >&2
41+
usage
42+
exit
43+
;;
44+
esac
45+
done
46+
47+
# Strip any options from the command line arguments now that
48+
# we've finished processing them
49+
shift "$((OPTIND-1))"
50+
51+
if [ $USING_DIFF -eq 1 ]; then
52+
# Check both staged and non-staged changes
53+
for path in $(git diff HEAD --name-only); do
54+
filename=$(basename "$path")
55+
file_extension="${filename##*.}"
56+
57+
# If an extension is present, and it's something other than 'py',
58+
# then ignore this file
59+
if [[ -n ${file_extension+x} && $file_extension != "py" ]]; then
60+
continue
61+
fi
62+
63+
# Append this path to our list of files to lint
64+
files+=("$path")
65+
done
66+
fi
67+
68+
# Append any remaining arguments as files to lint
69+
files+=("$@")
70+
71+
if [[ $USING_DIFF -eq 1 ]]; then
72+
# If we were asked to lint changed files, and no paths were found as a result...
73+
if [ ${#files[@]} -eq 0 ]; then
74+
# Then print and exit
75+
echo "No files found to lint."
76+
exit 0
77+
fi
1378
else
14-
files="synapse tests scripts-dev scripts contrib synctl"
79+
# If we were not asked to lint changed files, and no paths were found as a result,
80+
# then lint everything!
81+
if [[ -z ${files+x} ]]; then
82+
# Lint all source code files and directories
83+
files=("synapse" "tests" "scripts-dev" "scripts" "contrib" "synctl" "setup.py")
84+
fi
1585
fi
1686

17-
echo "Linting these locations: $files"
18-
isort $files
19-
python3 -m black $files
87+
echo "Linting these paths: ${files[*]}"
88+
echo
89+
90+
# Print out the commands being run
91+
set -x
92+
93+
isort "${files[@]}"
94+
python3 -m black "${files[@]}"
2095
./scripts-dev/config-lint.sh
21-
flake8 $files
96+
flake8 "${files[@]}"

setup.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
18-
1918
import glob
2019
import os
21-
from setuptools import setup, find_packages, Command
22-
import sys
2320

21+
from setuptools import Command, find_packages, setup
2422

2523
here = os.path.abspath(os.path.dirname(__file__))
2624

0 commit comments

Comments
 (0)