codeformat 脚本优化建议
#3118
-
codeformat.sh #!/usr/bin/env bash
if [[ $(uname) == 'Darwin' ]]; then
MAC_REQUIRED_TOOLS="python3 pipx"
for TOOL in ${MAC_REQUIRED_TOOLS[@]}; do
if [ ! $(which $TOOL) ]; then
if [ ! $(which brew) ]; then
echo "Homebrew not found. Trying to install..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ||
exit 1
fi
echo "$TOOL not found. Trying to install..."
brew install $TOOL || exit 1
fi
done
export PATH=${HOME}/.local/bin:/opt/homebrew/bin:$PATH
CLANG_FORMAT_VERSION=`clang-format --version`
if [[ "${CLANG_FORMAT_VERSION}" =~ "14." ]]
then
echo "----${CLANG_FORMAT_VERSION}----"
else
echo "----install clang-format----"
pipx install clang-format==14
fi
fi
echo "----begin to scan code format----"
find include/ -iname '*.h' -print0 | xargs clang-format -i
# shellcheck disable=SC2038
find src -name "*.cpp" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
# shellcheck disable=SC2038
find test \( -path test/framework/lzma \) -prune -o -name "*.cpp" -print -o -name "*.h" -print | xargs clang-format -i
find viewer/src -name "*.cpp" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print -o -name "*.hpp" -print | xargs clang-format -i
find exporter/src -name "*.cpp" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print -o -name "*.hpp" -print | xargs clang-format -i
git diff
result=`git diff`
if [[ $result =~ "diff" ]]
then
echo "----Failed to pass coding specification----"
exit 1
else
echo "----Pass coding specification----"
fi
echo "----Complete the scan code format-----"
也可以通过 git hooks 在提交时自动格式化 #!/bin/bash
export PATH=${HOME}/.local/bin:/opt/homebrew/bin:$PATH
CPP_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.(h|hpp|c|cpp|m|mm)$')
if [ -z "$CPP_FILES" ]; then
exit 0
fi
if [ -n "$CPP_FILES" ]; then
clang-format -i $CPP_FILES
git add $CPP_FILES
fi
exit 0将上面的文件保存到项目目录中如 配置 git chmod +x git_hooks/pre-commit.sh
cd .git/hooks
ln -s ../../git_hooks/pre-commit.sh pre-commit |
Beta Was this translation helpful? Give feedback.
Answered by
Hparty
Mar 3, 2026
Replies: 1 comment 2 replies
-
|
感谢建议,目前已经更新codeformat脚本 #3299 |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
Hparty
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
感谢建议,目前已经更新codeformat脚本 #3299