Add script to install clang-format pre-commit hook and update VSCode settings

This commit is contained in:
Eduard Iten 2025-07-09 08:47:55 +02:00
parent 067afdc237
commit 9db66c1246
2 changed files with 146 additions and 1 deletions

85
.vscode/settings.json vendored
View File

@ -2,5 +2,88 @@
"C_Cpp.default.compileCommands": "${workspaceFolder}/build/compile_commands.json",
"C_Cpp.default.cppStandard": "c++17",
"C_Cpp.default.cStandard": "c11",
"C_Cpp.default.intelliSenseMode": "linux-gcc-x64"
"C_Cpp.default.intelliSenseMode": "linux-gcc-x64",
"C_Cpp.clang_format_style": "file",
"C_Cpp.clang_format_fallbackStyle": "none",
"C_Cpp.clang_format_path": "",
"C_Cpp.vcFormat.indent.braces": false,
"C_Cpp.vcFormat.indent.multiLineRelativeToOpeningBrace": false,
"C_Cpp.vcFormat.indent.withinParentheses": "indent",
"C_Cpp.vcFormat.indent.preserveWithinParentheses": false,
"C_Cpp.vcFormat.indent.caseLabels": false,
"C_Cpp.vcFormat.indent.caseContents": true,
"C_Cpp.vcFormat.indent.caseContentsWhenBlock": false,
"C_Cpp.vcFormat.indent.lambdaBracesWhenParameter": true,
"C_Cpp.vcFormat.indent.gotoLabels": "leftmostColumn",
"C_Cpp.vcFormat.indent.preprocessor": "leftmostColumn",
"C_Cpp.vcFormat.indent.accessSpecifiers": false,
"C_Cpp.vcFormat.indent.namespaceContents": true,
"C_Cpp.vcFormat.newLine.beforeOpenBrace.namespace": "newLine",
"C_Cpp.vcFormat.newLine.beforeOpenBrace.type": "newLine",
"C_Cpp.vcFormat.newLine.beforeOpenBrace.function": "newLine",
"C_Cpp.vcFormat.newLine.beforeOpenBrace.block": "newLine",
"C_Cpp.vcFormat.newLine.beforeOpenBrace.lambda": "newLine",
"C_Cpp.vcFormat.newLine.beforeElse": false,
"C_Cpp.vcFormat.newLine.beforeCatch": true,
"C_Cpp.vcFormat.newLine.beforeWhileInDoWhile": false,
"C_Cpp.vcFormat.space.beforeFunctionOpenParenthesis": "remove",
"C_Cpp.vcFormat.space.withinParameterListParentheses": false,
"C_Cpp.vcFormat.space.betweenEmptyParameterListParentheses": false,
"C_Cpp.vcFormat.space.afterKeywordsInControlFlowStatements": true,
"C_Cpp.vcFormat.space.withinControlFlowStatementParentheses": false,
"C_Cpp.vcFormat.space.beforeLambdaOpenParenthesis": false,
"C_Cpp.vcFormat.space.withinCastParentheses": false,
"C_Cpp.vcFormat.space.afterCastCloseParenthesis": false,
"C_Cpp.vcFormat.space.withinExpressionParentheses": false,
"C_Cpp.vcFormat.space.beforeBlockOpenBrace": true,
"C_Cpp.vcFormat.space.betweenEmptyBraces": false,
"C_Cpp.vcFormat.space.beforeInitializerListOpenBrace": false,
"C_Cpp.vcFormat.space.withinInitializerListBraces": true,
"C_Cpp.vcFormat.space.preserveInInitializerList": true,
"C_Cpp.vcFormat.space.beforeOpenSquareBracket": false,
"C_Cpp.vcFormat.space.withinSquareBrackets": false,
"C_Cpp.vcFormat.space.beforeEmptySquareBrackets": false,
"C_Cpp.vcFormat.space.betweenEmptySquareBrackets": false,
"C_Cpp.vcFormat.space.groupSquareBrackets": true,
"C_Cpp.vcFormat.space.withinLambdaBrackets": false,
"C_Cpp.vcFormat.space.betweenEmptyLambdaBrackets": false,
"C_Cpp.vcFormat.space.beforeComma": false,
"C_Cpp.vcFormat.space.afterComma": true,
"C_Cpp.vcFormat.space.removeAroundMemberOperators": true,
"C_Cpp.vcFormat.space.beforeInheritanceColon": true,
"C_Cpp.vcFormat.space.beforeConstructorColon": true,
"C_Cpp.vcFormat.space.removeBeforeSemicolon": true,
"C_Cpp.vcFormat.space.insertAfterSemicolon": true,
"C_Cpp.vcFormat.space.removeAroundUnaryOperator": true,
"C_Cpp.vcFormat.space.aroundBinaryOperator": "insert",
"C_Cpp.vcFormat.space.aroundAssignmentOperator": "insert",
"C_Cpp.vcFormat.space.pointerReferenceAlignment": "right",
"C_Cpp.vcFormat.space.aroundTernaryOperator": "insert",
"C_Cpp.vcFormat.wrap.preserveBlocks": "oneLiners",
"editor.formatOnSave": true,
"editor.formatOnType": true,
"editor.rulers": [100],
"editor.tabSize": 8,
"editor.insertSpaces": false,
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"[c]": {
"editor.defaultFormatter": "ms-vscode.cpptools",
"editor.formatOnSave": true,
"editor.tabSize": 8,
"editor.insertSpaces": false
},
"[cpp]": {
"editor.defaultFormatter": "ms-vscode.cpptools",
"editor.formatOnSave": true,
"editor.tabSize": 8,
"editor.insertSpaces": false
},
"[h]": {
"editor.defaultFormatter": "ms-vscode.cpptools",
"editor.formatOnSave": true,
"editor.tabSize": 8,
"editor.insertSpaces": false
}
}

View File

@ -0,0 +1,62 @@
#!/bin/bash
# Function to print error messages in red
print_error() {
echo -e "\033[0;31mError: $1\033[0m"
}
# Check if clang-format is available
if ! command -v clang-format &> /dev/null
then
print_error "clang-format is not installed or not in your PATH. Please install it to use this pre-commit hook."
exit 1
fi
# Define the pre-commit hook content
HOOK_CONTENT='''#!/bin/bash
# Function to print error messages in red
print_error() {
echo -e "\033[0;31mError: $1\033[0m"
}
# Check if clang-format is available
if ! command -v clang-format &> /dev/null
then
print_error "clang-format is not installed or not in your PATH. Cannot format files."
exit 1
fi
# Get all staged C/C++/Objective-C/C++ files
FILES_TO_FORMAT=$(git diff --cached --name-only --diff-filter=ACM | grep -E "\.(c|h|cpp|hpp|cc|hh|cxx|hxx|m|mm)$")
if [ -z "$FILES_TO_FORMAT" ]; then
exit 0
fi
for FILE in $FILES_TO_FORMAT
do
# Check if the file exists and is a regular file
if [ -f "$FILE" ]; then
# Format the file and check for changes
clang-format -style=file -i "$FILE"
if ! git diff --quiet "$FILE"; then
git add "$FILE"
echo "Formatted $FILE with clang-format."
fi
fi
done
exit 0
'''
# Install the pre-commit hook
HOOK_PATH=".git/hooks/pre-commit"
# Create the hooks directory if it doesn't exist
mkdir -p .git/hooks
echo "$HOOK_CONTENT" > "$HOOK_PATH"
chmod +x "$HOOK_PATH"
echo "Git pre-commit hook for clang-format installed successfully."