Skip to content

Commit 0caf3e4

Browse files
committed
static-checks: fix getting modified files with staged state.
On lib-common, we always check the whole repository except when using our own git hook with staged files. Do not check for modified files when not in staged-files mode. 'HEAD^' might not be available on certain cases. Change-Id: I0d66f72e8a473571c494e8afacf2e1ae20dcff99 Priv-Id: 6dc534b61224dcfa5d734b83dd84e698011cf734
1 parent 1cc07c7 commit 0caf3e4

1 file changed

Lines changed: 11 additions & 21 deletions

File tree

static-checks.sh

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,22 @@ run_cmd() {
3535
"$@"
3636
}
3737

38-
modified_python_files() {
39-
local diff_mode="$1"
40-
local git_diff_files
38+
staged_python_files() {
4139
# Added, Copied, Modified, Renamed (we don't need to check deleted files
4240
# for instance).
4341
local diff_filter="--diff-filter=ACMR"
44-
if [ "$diff_mode" = "staged-files" ]; then
45-
# Get only the staged files
46-
git_diff_files="git diff-index --name-only $diff_filter --cached HEAD"
47-
else
48-
# By default, we check the last commit
49-
git_diff_files="git diff $diff_filter --name-only HEAD^"
50-
fi
5142
# Filter only the existing files and not the removed ones
5243
# Convert new lines into spaces for nice printing
5344
# With xargs || true, we ensure that if any file does not really exist we
5445
# skip outputting just that file.
55-
eval "$git_diff_files -- \
56-
'*.py' '**/*.py' '*.pyi' '**/*.pyi' 'wscript*' '**/wscript*' | \
57-
(xargs --no-run-if-empty stat --printf '%n ' 2>/dev/null || true)"
46+
git diff-index --name-only $diff_filter --cached HEAD -- \
47+
'*.py' '**/*.py' '*.pyi' '**/*.pyi' 'wscript*' '**/wscript*' | \
48+
(xargs --no-run-if-empty stat --printf '%n ' 2>/dev/null || true)
5849
}
5950

6051
main() {
6152
local params
62-
local modified_files
53+
local staged_files
6354
local diff_mode="last-commit"
6455
local supported_commands="help,staged-files"
6556

@@ -93,20 +84,19 @@ main() {
9384
esac
9485
done
9586

96-
modified_files="$(modified_python_files "$diff_mode")"
97-
9887
if [[ "$diff_mode" = "staged-files" ]]; then
9988
# This diff_mode is used by our git hook.
89+
staged_files="$(staged_python_files)"
10090

101-
if [[ -n "$modified_files" ]]; then
102-
# Only run linters on modified_files, otherwise it will run on
91+
if [[ -n "$staged_files" ]]; then
92+
# Only run linters on staged_files, otherwise it will run on
10393
# file modified locally but not staged.
10494

105-
# Intended splitting of modified_files
95+
# Intended splitting of staged_files
10696
# shellcheck disable=SC2086
107-
run_cmd ruff check --force-exclude ${modified_files}
97+
run_cmd ruff check --force-exclude ${staged_files}
10898
# shellcheck disable=SC2086
109-
run_cmd waf mypy ${modified_files}
99+
run_cmd waf mypy ${staged_files}
110100
fi
111101
else
112102
# The bot executes static-check without setting any diff_mode.

0 commit comments

Comments
 (0)