Skip to content

Commit

Permalink
Add clang-format rules and scripts
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bastienjacquet committed Jun 29, 2017
1 parent 4905407 commit 95deaa3
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .clang-format
@@ -0,0 +1,10 @@
---
# This configuration requires clang-format 3.8 or higher.
BasedOnStyle: Mozilla
AlignAfterOpenBracket: DontAlign
AlignOperands: false
AlwaysBreakAfterReturnType: None
AlwaysBreakAfterDefinitionReturnType: None
BreakBeforeBraces: Allman
ColumnLimit: 100
Standard: Cpp03
2 changes: 2 additions & 0 deletions .gitattributes
@@ -1,2 +1,4 @@
Superbuild/Projects/patches/*.patch -text
*.pdf binary
*.h format.clang-format
*.cxx format.clang-format
118 changes: 118 additions & 0 deletions Utilities/Scripts/clang-format.bash
@@ -0,0 +1,118 @@
#!/usr/bin/env bash
#=============================================================================
# Copyright 2015-2016 Kitware, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================

usage='usage: clang-format.bash [<options>] [--]
--help Print usage plus more detailed help.
--clang-format <tool> Use given clang-format tool.
--amend Filter files changed by HEAD.
--cached Filter files locally staged for commit.
--modified Filter files locally modified from HEAD.
--tracked Filter files tracked by Git.
'

help="$usage"'
Example to format locally modified files:
Utilities/Scripts/clang-format.bash --modified
Example to format locally modified files staged for commit:
Utilities/Scripts/clang-format.bash --cached
Example to format files modified by the most recent commit:
Utilities/Scripts/clang-format.bash --amend
Example to format all files tracked by Git:
Utilities/Scripts/clang-format.bash --tracked
Example to format the current topic:
git filter-branch \
--tree-filter "Utilities/Scripts/clang-format.bash --tracked" \
master..
'

die() {
echo "$@" 1>&2; exit 1
}

#-----------------------------------------------------------------------------

# Parse command-line arguments.
clang_format=''
mode=''
while test "$#" != 0; do
case "$1" in
--amend) mode="amend" ;;
--cached) mode="cached" ;;
--clang-format) shift; clang_format="$1" ;;
--help) echo "$help"; exit 0 ;;
--modified) mode="modified" ;;
--tracked) mode="tracked" ;;
--) shift ; break ;;
-*) die "$usage" ;;
*) break ;;
esac
shift
done
test "$#" = 0 || die "$usage"

# Find a default tool.
tools='
clang-format
clang-format-3.8
'
if test "x$clang_format" = "x"; then
for tool in $tools; do
if type -p "$tool" >/dev/null; then
clang_format="$tool"
break
fi
done
fi

# Verify that we have a tool.
if ! type -p "$clang_format" >/dev/null; then
echo "Unable to locate '$clang_format'"
exit 1
fi

# Select listing mode.
case "$mode" in
'') echo "$usage"; exit 0 ;;
amend) git_ls='git diff-tree --diff-filter=AM --name-only HEAD -r --no-commit-id' ;;
cached) git_ls='git diff-index --diff-filter=AM --name-only HEAD --cached' ;;
modified) git_ls='git diff-index --diff-filter=AM --name-only HEAD' ;;
tracked) git_ls='git ls-files' ;;
*) die "invalid mode: $mode" ;;
esac
echo `$git_ls | git check-attr --stdin format.clang-format| sed -n '/: format\.clang-format: set$/ {s/:[^:]*:[^:]*$//p}'`
# List files as selected above.
$git_ls |

# Select sources with our attribute.
git check-attr --stdin format.clang-format |
sed -n '/: format\.clang-format: set$/ {s/:[^:]*:[^:]*$//p}' |

# Update sources in-place.
xargs -d '\n' "$clang_format" -i

23 changes: 23 additions & 0 deletions Utilities/Scripts/pre-commit.hook
@@ -0,0 +1,23 @@
#!/bin/sh
#
has_unstaged_changes=`git diff --ignore-submodules=all`
[ ! -z "$has_unstaged_changes" ] && git stash --keep-index
./Utilities/Scripts/clang-format.bash --cached
has_reformatted=`git diff --ignore-submodules=all`
if [ ! -z "$has_reformatted" ]; then
echo "Your staged changes are ill-formated."
if [ -z "$has_unstaged_changes" ]; then
git add -u
echo " Reformated changes have been staged. Please review."
exit 1
else
git checkout . --quiet
git stash pop --index --quiet
echo " You have unstaged changes, so we cannot automatically reformat."
echo " Please manually run \"./Utilities/Scripts/clang-format.bash --cached\","
echo " and review the changes."
exit 1
fi
fi
[ ! -z "$has_unstaged_changes" ] && git stash pop --index
exit 0

0 comments on commit 95deaa3

Please sign in to comment.