From f618f68fa61c240dd456e0af4e86ee2650d4430e Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Sat, 22 Aug 2015 18:47:13 +0200 Subject: [PATCH] + pre-commit hook and code style configs --- .clang-format | 23 ++++++++++ .pre-commit | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ .uncrustify.cfg | 6 +-- 3 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 .clang-format create mode 100755 .pre-commit diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000..78e1c6a25c --- /dev/null +++ b/.clang-format @@ -0,0 +1,23 @@ +Standard: Cpp11 + +IndentWidth: 8 +ColumnLimit: 0 +UseTab: Always +MaxEmptyLinesToKeep: 2 + +BreakBeforeBraces: Attach + +AllowShortIfStatementsOnASingleLine: true +AllowShortLoopsOnASingleLine: true + +IndentCaseLabels: false +NamespaceIndentation: None +AccessModifierOffset: -8 + +PointerAlignment: Left + +SpacesInAngles: true +SpacesInParentheses: true +SpaceInEmptyParentheses: true +# SpacesInSquareBrackets: true +SpaceBeforeParens: Always diff --git a/.pre-commit b/.pre-commit new file mode 100755 index 0000000000..852ce2f126 --- /dev/null +++ b/.pre-commit @@ -0,0 +1,120 @@ +#!/bin/sh + +# git pre-commit hook that runs an Uncrustify stylecheck. +# Features: +# - abort commit when commit does not comply with the style guidelines +# - create a patch of the proposed style changes +# +# More info on Uncrustify: http://uncrustify.sourceforge.net/ + +# This file is part of a set of unofficial pre-commit hooks available +# at github. +# Link: https://github.com/githubbrowser/Pre-commit-hooks +# Contact: David Martin, david.martin.mailbox@googlemail.com + + +################################################################## +# SETTINGS +# set path to uncrustify binary +# UNCRUSTIFY="/usr/bin/uncrustify" +UNCRUSTIFY="uncrustify" + +# set uncrustify config location +# CONFIG="/home/user/.config/uncrustify.cfg" +CONFIG=".uncrustify.cfg" + +# the source language: C, CPP, D, CS, JAVA, PAWN, VALA, OC, OC+ +SOURCE_LANGUAGE="CPP" + +# remove any older patches from previous commits. Set to true or false. +# DELETE_OLD_PATCHES=false +DELETE_OLD_PATCHES=false + +# only parse files with the extensions in FILE_EXTS. Set to true or false. +# if false every changed file in the commit will be parsed with Uncrustify. +# if true only files matching one of the extensions are parsed with Uncrustify. +# PARSE_EXTS=true +PARSE_EXTS=true + +# file types to parse. Only effective when PARSE_EXTS is true. +# FILE_EXTS=".c .h .cpp .hpp" +FILE_EXTS=".c .h .cpp .hpp .cxx" + +################################################################## +# There should be no need to change anything below this line. + +# check whether the given file matches any of the set extensions +matches_extension() { + local filename="$(basename -- "$1")" + local extension=".${filename##*.}" + local ext + + for ext in $FILE_EXTS; do [ "$ext" = "$extension" ] && return 0; done + + return 1 +} + +# necessary check for initial commit +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 + +# make sure the config file and executable are correctly set +if [ ! -f "$CONFIG" ] ; then + printf "Error: uncrustify config file not found.\n" + printf "Set the correct path in $(canonicalize_filename "$0").\n" + exit 1 +fi + +# create a random filename to store our generated patch +prefix="pre-commit-uncrustify" +suffix="$(date +%s)" +patch="/tmp/$prefix-$suffix.patch" + +# clean up any older uncrustify patches +$DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch + +# create one patch containing all changes to the files +git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file; +do + # ignore file if we do check for file extensions and the file + # does not match any of the extensions specified in $FILE_EXTS + if $PARSE_EXTS && ! matches_extension "$file"; then + continue; + fi + + # uncrustify our sourcefile, create a patch with diff and append it to our $patch + # The sed call is necessary to transform the patch from + # --- $file timestamp + # +++ - timestamp + # to both lines working on the same file and having a a/ and b/ prefix. + # Else it can not be applied with 'git apply'. + clang-format "$file" | \ + "$UNCRUSTIFY" -c "$CONFIG" -l "$SOURCE_LANGUAGE" | \ + diff -u -- "$file" - | \ + sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch" +done + +# if no patch has been generated all is ok, clean up the file stub and exit +if [ ! -s "$patch" ] ; then + printf "Files in this commit comply with the uncrustify rules.\n" + rm -f "$patch" + + # If there are whitespace errors, print the offending file names and fail. + exec git diff-index --check --cached $against -- +fi + +# a patch has been created, notify the user and exit +printf "\nThe following differences were found between the code to commit " +printf "and the uncrustify rules:\n\n" +cat "$patch" + +printf "\nYou can apply these changes with:\n git apply $patch\n" +printf "(may need to be called from the root directory of your repository)\n" +printf "Aborting commit. Apply changes and commit again or skip checking with" +printf " --no-verify (not recommended).\n" + +exit 1 diff --git a/.uncrustify.cfg b/.uncrustify.cfg index ec33a24fd2..9c76136d39 100644 --- a/.uncrustify.cfg +++ b/.uncrustify.cfg @@ -180,7 +180,7 @@ newlines = lf # auto / lf / crlf / cr indent_label = 1 # number # Same as indent_label, but for access specifiers that are followed by a colon - indent_access_spec = 1 # number + indent_access_spec = -4 # number # Indent the code after an access specifier by one level. # If set, this option forces 'indent_access_spec=0' @@ -591,14 +591,14 @@ newlines = lf # auto / lf / crlf / cr # Add or remove space after the '&' (address-of) operator. Default=Remove # This does not affect the spacing after a '&' that is part of a type. - sp_addr = remove # ignore / add / remove / force + sp_addr = force # ignore / add / remove / force # Add or remove space around the '.' or '->' operators. Default=Remove sp_member = remove # ignore / add / remove / force # Add or remove space after the '*' (dereference) operator. Default=Remove # This does not affect the spacing after a '*' that is part of a type. - sp_deref = remove # ignore / add / remove / force + sp_deref = force # ignore / add / remove / force # Add or remove space after '+' or '-', as in 'x = -5' or 'y = +7'. Default=Remove sp_sign = remove # ignore / add / remove / force -- GitLab