|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | 3 | ################################################################################ |
4 | | -# Formats python files that have been modified. |
| 4 | +# Formats python and notebook files that have been modified. |
5 | 5 | # |
6 | 6 | # Usage: |
7 | 7 | # check/format-incremental [BASE_REVISION] [--apply] [--all] |
8 | 8 | # |
9 | | -# By default, the script analyzes python files that have changed relative to the |
10 | | -# base revision and determines whether they need to be formatted. If any changes |
11 | | -# are needed, it prints the diff and exits with code 1, otherwise it exits with |
12 | | -# code 0. |
| 9 | +# By default, the script analyzes python and notebook files that have changed |
| 10 | +# relative to the base revision and determines whether they need to be |
| 11 | +# formatted. If any changes are needed, it prints the diff and exits with |
| 12 | +# code 1, otherwise it exits with code 0. |
13 | 13 | # |
14 | 14 | # With '--apply', reformats the files instead of printing the diff and exits |
15 | 15 | # with code 0. |
16 | 16 | # |
17 | | -# With '--all', analyzes all python files, instead of only changed files. |
| 17 | +# With '--all', analyzes all files, instead of only changed files. |
18 | 18 | # |
19 | 19 | # You can specify a base git revision to compare against (i.e. to use when |
20 | 20 | # determining whether or not a file is considered to have "changed"). For |
@@ -80,25 +80,25 @@ if (( only_changed == 1 )); then |
80 | 80 | rev="${base}" |
81 | 81 | fi |
82 | 82 |
|
83 | | - # Get the modified, added and moved python files. |
| 83 | + # Get the modified, added and moved python and notebook files. |
84 | 84 | IFS=$'\n' read -r -d '' -a format_files < \ |
85 | 85 | <(git diff --name-only --diff-filter=MAR "${rev}" -- \ |
86 | | - '*.py' ':(exclude)*_pb2.py') |
| 86 | + '*.py' '*.ipynb' ':(exclude)*_pb2.py') |
87 | 87 | else |
88 | | - echo -e "Formatting all python files." >&2 |
| 88 | + echo -e "Formatting all python and notebook files." >&2 |
89 | 89 | IFS=$'\n' read -r -d '' -a format_files < \ |
90 | | - <(git ls-files '*.py' ':(exclude)*_pb2.py') |
| 90 | + <(git ls-files '*.py' '*.ipynb' ':(exclude)*_pb2.py') |
91 | 91 | fi |
92 | 92 |
|
93 | 93 | if (( ${#format_files[@]} == 0 )); then |
94 | 94 | echo -e "\033[32mNo files to format.\033[0m" |
95 | 95 | exit 0 |
96 | 96 | fi |
97 | 97 |
|
98 | | -# Exclude __init__.py files from isort targets |
| 98 | +# Apply isort only on python files with the exception of __init__.py files |
99 | 99 | declare -a isort_files=() |
100 | 100 | for f in "${format_files[@]}"; do |
101 | | - if [[ "${f##*/}" != __init__.py ]]; then |
| 101 | + if [[ "${f}" == *.py && "${f##*/}" != __init__.py ]]; then |
102 | 102 | isort_files+=("${f}") |
103 | 103 | fi |
104 | 104 | done |
|
0 commit comments