Skip to content

Commit 95deaa3

Browse files
Add clang-format rules and scripts
This require the developer to install the clang-format program. Please run localy the following script before any commit: ./Utilities/Scripts/clang-format.bash You can also tell git to automatically do it, by copying the following local pre-commit hook: cp ./Utilities/Scripts/pre-commit.hook .git/hooks/pre-commit
1 parent 4905407 commit 95deaa3

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

.clang-format

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
# This configuration requires clang-format 3.8 or higher.
3+
BasedOnStyle: Mozilla
4+
AlignAfterOpenBracket: DontAlign
5+
AlignOperands: false
6+
AlwaysBreakAfterReturnType: None
7+
AlwaysBreakAfterDefinitionReturnType: None
8+
BreakBeforeBraces: Allman
9+
ColumnLimit: 100
10+
Standard: Cpp03

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Superbuild/Projects/patches/*.patch -text
22
*.pdf binary
3+
*.h format.clang-format
4+
*.cxx format.clang-format
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/env bash
2+
#=============================================================================
3+
# Copyright 2015-2016 Kitware, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#=============================================================================
17+
18+
usage='usage: clang-format.bash [<options>] [--]
19+
20+
--help Print usage plus more detailed help.
21+
22+
--clang-format <tool> Use given clang-format tool.
23+
24+
--amend Filter files changed by HEAD.
25+
--cached Filter files locally staged for commit.
26+
--modified Filter files locally modified from HEAD.
27+
--tracked Filter files tracked by Git.
28+
'
29+
30+
help="$usage"'
31+
Example to format locally modified files:
32+
33+
Utilities/Scripts/clang-format.bash --modified
34+
35+
Example to format locally modified files staged for commit:
36+
37+
Utilities/Scripts/clang-format.bash --cached
38+
39+
Example to format files modified by the most recent commit:
40+
41+
Utilities/Scripts/clang-format.bash --amend
42+
43+
Example to format all files tracked by Git:
44+
45+
Utilities/Scripts/clang-format.bash --tracked
46+
47+
Example to format the current topic:
48+
49+
git filter-branch \
50+
--tree-filter "Utilities/Scripts/clang-format.bash --tracked" \
51+
master..
52+
'
53+
54+
die() {
55+
echo "$@" 1>&2; exit 1
56+
}
57+
58+
#-----------------------------------------------------------------------------
59+
60+
# Parse command-line arguments.
61+
clang_format=''
62+
mode=''
63+
while test "$#" != 0; do
64+
case "$1" in
65+
--amend) mode="amend" ;;
66+
--cached) mode="cached" ;;
67+
--clang-format) shift; clang_format="$1" ;;
68+
--help) echo "$help"; exit 0 ;;
69+
--modified) mode="modified" ;;
70+
--tracked) mode="tracked" ;;
71+
--) shift ; break ;;
72+
-*) die "$usage" ;;
73+
*) break ;;
74+
esac
75+
shift
76+
done
77+
test "$#" = 0 || die "$usage"
78+
79+
# Find a default tool.
80+
tools='
81+
clang-format
82+
clang-format-3.8
83+
'
84+
if test "x$clang_format" = "x"; then
85+
for tool in $tools; do
86+
if type -p "$tool" >/dev/null; then
87+
clang_format="$tool"
88+
break
89+
fi
90+
done
91+
fi
92+
93+
# Verify that we have a tool.
94+
if ! type -p "$clang_format" >/dev/null; then
95+
echo "Unable to locate '$clang_format'"
96+
exit 1
97+
fi
98+
99+
# Select listing mode.
100+
case "$mode" in
101+
'') echo "$usage"; exit 0 ;;
102+
amend) git_ls='git diff-tree --diff-filter=AM --name-only HEAD -r --no-commit-id' ;;
103+
cached) git_ls='git diff-index --diff-filter=AM --name-only HEAD --cached' ;;
104+
modified) git_ls='git diff-index --diff-filter=AM --name-only HEAD' ;;
105+
tracked) git_ls='git ls-files' ;;
106+
*) die "invalid mode: $mode" ;;
107+
esac
108+
echo `$git_ls | git check-attr --stdin format.clang-format| sed -n '/: format\.clang-format: set$/ {s/:[^:]*:[^:]*$//p}'`
109+
# List files as selected above.
110+
$git_ls |
111+
112+
# Select sources with our attribute.
113+
git check-attr --stdin format.clang-format |
114+
sed -n '/: format\.clang-format: set$/ {s/:[^:]*:[^:]*$//p}' |
115+
116+
# Update sources in-place.
117+
xargs -d '\n' "$clang_format" -i
118+

Utilities/Scripts/pre-commit.hook

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/sh
2+
#
3+
has_unstaged_changes=`git diff --ignore-submodules=all`
4+
[ ! -z "$has_unstaged_changes" ] && git stash --keep-index
5+
./Utilities/Scripts/clang-format.bash --cached
6+
has_reformatted=`git diff --ignore-submodules=all`
7+
if [ ! -z "$has_reformatted" ]; then
8+
echo "Your staged changes are ill-formated."
9+
if [ -z "$has_unstaged_changes" ]; then
10+
git add -u
11+
echo " Reformated changes have been staged. Please review."
12+
exit 1
13+
else
14+
git checkout . --quiet
15+
git stash pop --index --quiet
16+
echo " You have unstaged changes, so we cannot automatically reformat."
17+
echo " Please manually run \"./Utilities/Scripts/clang-format.bash --cached\","
18+
echo " and review the changes."
19+
exit 1
20+
fi
21+
fi
22+
[ ! -z "$has_unstaged_changes" ] && git stash pop --index
23+
exit 0

0 commit comments

Comments
 (0)