也许我错过了什么,但仍然没有找到这样的设置。正式地说clang-format
没有生成正确的UNIX文本文件,因为最后一行总是缺少EOL字符。
答案 0 :(得分:1)
选项 1:
发现和外部参考。这可以帮助您,“您可以递归地添加 EOL 字符/清理来自 here... 的文件”
git ls-files -z "*.cpp" "*.hpp" | while IFS= read -rd '' f; do tail -c1 < "$f" | read -r _ || echo >> "$f"; done
说明:
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.
来自 Clang format script 的选项 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