Skip to content

Commit 7995daf

Browse files
pavoljuhasBichengYing
authored andcommitted
Fix isort invocation in format-incremental (quantumlib#7194)
* format-incremental - include files in cirq-google/cirq_google/cloud These files are already formatted by black. * format-incremental - exclude `__init__.py` files from isort targets Skip isort execution altogether if only `__init__.py` files changed.
1 parent 58f5155 commit 7995daf

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

check/format-incremental

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ for arg in "$@"; do
5757
fi
5858
done
5959

60-
typeset -a format_files
60+
declare -a format_files=()
6161
if (( only_changed == 1 )); then
6262
# Figure out which branch to compare against.
6363
if [ -z "${rev}" ]; then
@@ -82,19 +82,28 @@ if (( only_changed == 1 )); then
8282

8383
# Get the modified, added and moved python files.
8484
IFS=$'\n' read -r -d '' -a format_files < \
85-
<(git diff --name-only --diff-filter=MAR "${rev}" -- '*.py' ':(exclude)cirq-google/cirq_google/cloud/*' ':(exclude)*_pb2.py')
85+
<(git diff --name-only --diff-filter=MAR "${rev}" -- \
86+
'*.py' ':(exclude)*_pb2.py')
8687
else
8788
echo -e "Formatting all python files." >&2
8889
IFS=$'\n' read -r -d '' -a format_files < \
8990
<(git ls-files '*.py' ':(exclude)*_pb2.py')
9091
fi
9192

9293
if (( ${#format_files[@]} == 0 )); then
93-
echo -e "\033[32mNo files to format\033[0m."
94+
echo -e "\033[32mNo files to format.\033[0m"
9495
exit 0
9596
fi
9697

97-
# color the output if it goes to a terminal or GitHub Actions log
98+
# Exclude __init__.py files from isort targets
99+
declare -a isort_files=()
100+
for f in "${format_files[@]}"; do
101+
if [[ "${f##*/}" != __init__.py ]]; then
102+
isort_files+=("${f}")
103+
fi
104+
done
105+
106+
# Color the output if it goes to a terminal or GitHub Actions log
98107
arg_color=()
99108
if [[ -t 1 || "${CI}" == true ]]; then
100109
arg_color=("--color")
@@ -109,9 +118,11 @@ if (( only_print == 1 )); then
109118
args+=("--check" "--diff")
110119
fi
111120

112-
# TODO(#4863, #7181) - exclude __init__.py from isort arguments and enable
113-
echo "isort is temporarily disabled"
114-
ISORTSTATUS=$?
121+
ISORTSTATUS=0
122+
if (( "${#isort_files[@]}" )); then
123+
isort "${args[@]}" "${isort_files[@]}"
124+
ISORTSTATUS=$?
125+
fi
115126

116127
BLACKVERSION="$(black --version | head -1)"
117128

0 commit comments

Comments
 (0)