[icinga-checkins] icinga.org: icinga2/feature/clang-format: Add Git hook for clang-format

git at icinga.org git at icinga.org
Tue Aug 30 15:23:29 CEST 2016


Module: icinga2
Branch: feature/clang-format
Commit: d12acd505b6359c880f63eb51950af8338361dae
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=d12acd505b6359c880f63eb51950af8338361dae

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Tue Aug 30 05:44:20 2016 +0200

Add Git hook for clang-format

---

 .clang-format         |   39 +++++++++++++++++++++++++++++++++++++++
 .clang-format-exclude |    3 +++
 .clang-format-include |    1 +
 tools/git/pre-commit  |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 91 insertions(+)

diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..2b0e3ac
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,39 @@
+---
+Language: Cpp
+BasedOnStyle:  LLVM
+AlignAfterOpenBracket: false
+BreakBeforeBraces: Custom
+BraceWrapping:
+  AfterClass: true
+  AfterEnum: true
+  AfterFunction: true
+  AfterNamespace: true
+  AfterStruct: true
+  AfterUnion: true
+IndentWidth: 4
+ContinuationIndentWidth: 2
+AccessModifierOffset: -4
+IndentCaseLabels: true
+ReflowComments: false
+ColumnLimit: 100
+AlwaysBreakTemplateDeclarations: true
+IncludeCategories:
+  - Regex:           'i2-.\*.hpp"$'
+    Priority:        -1
+  - Regex:           '\.tcpp"$'
+    Priority:        1
+  - Regex:           '^"db_ido/'
+    Priority:        2
+  - Regex:           '^"icinga/'
+    Priority:        3
+  - Regex:           '^"(remote|cli)/'
+    Priority:        4
+  - Regex:           '^"config\/'
+    Priority:        5
+  - Regex:           '^"'
+    Priority:        6
+  - Regex:           '^<boost'
+    Priority:        7
+  - Regex:           '^<'
+    Priority:        8
+IncludeIsMainRegex: '$'
diff --git a/.clang-format-exclude b/.clang-format-exclude
new file mode 100644
index 0000000..5998a22
--- /dev/null
+++ b/.clang-format-exclude
@@ -0,0 +1,3 @@
+^third-party
+^icinga-studio/forms.h
+^(build|debug|release)
diff --git a/.clang-format-include b/.clang-format-include
new file mode 100644
index 0000000..aefe463
--- /dev/null
+++ b/.clang-format-include
@@ -0,0 +1 @@
+\.[ch](pp)?$
diff --git a/tools/git/pre-commit b/tools/git/pre-commit
new file mode 100755
index 0000000..afd69d7
--- /dev/null
+++ b/tools/git/pre-commit
@@ -0,0 +1,48 @@
+#!/usr/bin/env bash
+
+if git rev-parse --verify HEAD >/dev/null 2>&1
+then
+  against=HEAD
+else
+  # Initial commit: diff against an empty tree object
+  against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
+fi
+
+exec 1>&2
+
+if [ ! -f .clang-format ]; then
+  exit 0
+fi
+
+CLANG_FORMAT=$(which clang-format clang-format-mp-3.8 clang-format-mp-3.9 | head -1)
+
+if [ -z "$CLANG_FORMAT" ]; then
+  echo "Could not find clang-format."
+  exit 1
+fi
+
+for file in $(git diff --cached --name-only --diff-filter=ACM); do
+  include=0
+  for pattern in $(< .clang-format-include); do
+    if [[ $file =~ $pattern ]]; then
+      include=1
+      break
+    fi
+  done
+  for pattern in $(< .clang-format-exclude); do
+    if [[ $file =~ $pattern ]]; then
+      include=0
+      break
+    fi
+  done
+  if [ $include -eq 1 ]; then
+    if ! $CLANG_FORMAT --style=file -i $file; then
+      echo "clang-format failed to format file '$file'"
+      exit 1
+    fi
+
+    git add $file
+  fi
+done
+
+exit 0



More information about the icinga-checkins mailing list