Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeStyle] fix renamed files not being monitored by Codestyle Check #48892

Merged
merged 11 commits into from
Dec 9, 2022
2 changes: 1 addition & 1 deletion tools/codestyle/pre_commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ if ! [[ $(python -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $1$2}') -ge 36
exit 1
fi

diff_files=$(git diff --numstat ${BRANCH} | awk '{print $NF}')
diff_files=$(git diff --name-only --diff-filter=ACMR ${BRANCH})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前的 diff 文件是通过先 git diff --numstat 再经过 awk 处理得到的

这样对于大多数文件是没问题的,但对于 rename 文件是有问题的,因为 git diff --numstat 结果像这样:

> git diff --numstat --cached
1       0       python/paddle/fft.py
0       0       python/paddle/{hub.py => hubx.py}

这样 fft.py 通过 awk 计算的路径是正确的,而 rename 的 hub.py 则会计算出错误的路径

> git diff --numstat --cached | awk '{print $NF}'
python/paddle/fft.py
hubx.py}

为了避免这样的问题,调整了参数,直接得到需要的文件名

> git diff --name-only --cached --diff-filter=ACMR
python/paddle/fft.py
python/paddle/hubx.py

这里 --diff-filter ACMR 分别代表 Added (A)、Copied (C)、Modified (M)、Renamed (R) 1

Footnotes

  1. --diff-filter -- git-diff docs

num_diff_files=$(echo "$diff_files" | wc -l)
echo -e "diff files between pr and ${BRANCH}:\n${diff_files}"

Expand Down