提交 0e12b7c9 编写于 作者: 忠阳 提交者: xiaoyi.yl

add .clang-format and git-hooks

上级 875cd0c2
---
Language: Cpp
# BasedOnStyle: LLVM
SortIncludes: false
AccessModifierOffset: -2
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
DerivePointerAlignment: false
PointerAlignment: Left
ConstructorInitializerIndentWidth: 4
AlignEscapedNewlinesLeft: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: true
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
AlwaysBreakBeforeMultilineStrings: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BinPackParameters: true
ColumnLimit: 120
ConstructorInitializerAllOnOneLineOrOnePerLine: true
DerivePointerBinding: true
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: true
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 200
PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
PointerBindsToType: true
Cpp11BracedListStyle: true
Standard: Auto
IndentWidth: 2
TabWidth: 4
UseTab: Never
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
IndentFunctionDeclarationAfterType: true
SpacesInParentheses: false
SpacesInAngles: false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpaceAfterControlStatementKeyword: true
SpaceBeforeAssignmentOperators: true
SpacesBeforeTrailingComments: 2
ContinuationIndentWidth: 4
CommentPragmas: '^lint'
MacroBlockBegin: "
END_CATCH_ERROR$"
...
#!/bin/bash
set -e
ROOT=$(git rev-parse --show-toplevel)
ln -sf $ROOT/git-hooks/pre-commit $ROOT/.git/hooks/pre-commit
echo "installed git-hooks/pre-commit to .git/hooks/pre-commit"
#!/bin/bash
set -e
ROOT=$(git rev-parse --show-toplevel)
CLANG_FORMAT_HOOK_SCRIPT=$ROOT/git-hooks/pre-commit-clang-format
$CLANG_FORMAT_HOOK_SCRIPT
#!/bin/bash
CLANG_FORMAT=`which clang-format`
SOURCE_FILE_EXTS=(.h .hh .hpp .hxx .c .cc .cpp .cxx)
check_clang_format() {
if [ ! -x "$CLANG_FORMAT" ] ; then
echo "ERROR: clang-format executable not found."
exit 1
fi
}
# check whether the given file matches any of the extensions: case insensitive
matches_extension() {
local file_name=$(basename "$1")
local file_ext=".${file_name##*.}"
local lowercase_file_ext=`echo $file_ext | awk '{print tolower($0)}'`
local source_file_ext
for source_file_ext in "${SOURCE_FILE_EXTS[@]}"
do
local lowercase_source_file_ext=`echo $source_file_ext | awk '{print tolower($0)}'`
[[ "$lowercase_file_ext" = "$lowercase_source_file_ext" ]] && return 0
done
return 1
}
_FORMATTED_FILES_CNT=0
format_file() {
local source_file=$1
local formatted_file="${source_file}.formatted"
$CLANG_FORMAT -style=file $source_file > $formatted_file
cmp -s $source_file $formatted_file
if [ $? -ne 0 ]
then
mv $formatted_file $source_file && echo "formatted file $source_file"
let _FORMATTED_FILES_CNT++
else
rm -f $formatted_file
fi
}
_ROOT=$(git rev-parse --show-toplevel)
format_staged_source_files() {
for file in $(git diff --staged --name-only)
do
file=$_ROOT/$file
matches_extension $file && format_file $file
done
}
# return 1 if there was any file actually formatted, which will break the `git commit` process.
verify_format() {
if [ ${_FORMATTED_FILES_CNT} -gt 0 ]
then
echo "${_FORMATTED_FILES_CNT} files has been formatted."
exit 1
fi
}
main() {
check_clang_format
format_staged_source_files
verify_format
}
main
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册