也许我错过了一些东西,但是仍然没有找到这样的设置。从形式上讲,clang-format这不会产生正确的UNIX文本文件,因为最后几行始终缺少EOL字符。
选项1:
找到并外部参考。这可以帮助您,“您可以从这里递归地添加 EOL 字符/清理文件...... ”
git ls-files -z "*.cpp" "*.hpp" | while IFS= read -rd '' f; do tail -c1 < "$f" | read -r _ || echo >> "$f"; done
Run Code Online (Sandbox Code Playgroud)
解释:
git ls-files -z "*.cpp" "*.hpp" //lists files in the repository matching the listed patterns. You can add more patterns, but they need the quotes so that the * is not substituted by the shell but interpreted by git. As an alternative, you could use find -print0 ... or similar programs to list affected files - just make sure it emits NUL-delimited entries.
while IFS= read -rd '' f; do ... done //iterates through the entries, safely handling filenames that include whitespace and/or newlines.
tail -c1 < "$f" reads the last char from a file.
read -r _ exits with a nonzero exit status if a trailing newline is missing.
|| echo >> "$f" appends a newline to the file if the exit status of the previous command was nonzero.
Run Code Online (Sandbox Code Playgroud)
Clang格式脚本的选项 2 :
#!/bin/bash
set -e
function append_newline {
if [[ -z "$(tail -c 1 "$1")" ]]; then
:
else
echo >> "$1"
fi
}
if [ -z "$1" ]; then
TARGET_DIR="."
else
TARGET_DIR=$1
fi
pushd ${TARGET_DIR} >> /dev/null
# Find all source files using Git to automatically respect .gitignore
FILES=$(git ls-files "*.h" "*.cpp" "*.c")
# Run clang-format
clang-format-10 -i ${FILES}
# Check newlines
for f in ${FILES}; do
append_newline $f
done
popd >> /dev/null
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
237 次 |
| 最近记录: |