From b25ef28ade8836a45f960b5276fb37148b4d35bd Mon Sep 17 00:00:00 2001 From: Valentin Sawadski Date: Sat, 5 Oct 2013 10:39:47 +0200 Subject: [PATCH] feat(code-style): Add a script to automatically format all changed files If executed inside the repository it will run the default Contiki uncrustify script on all changed .c and .h file. This could be added as a Git pre-commit hook. See http://git-scm.com/book/en/Customizing-Git-Git-Hooks --- tools/code-style/uncrustify-changed.sh | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 tools/code-style/uncrustify-changed.sh diff --git a/tools/code-style/uncrustify-changed.sh b/tools/code-style/uncrustify-changed.sh new file mode 100755 index 000000000..e79accaec --- /dev/null +++ b/tools/code-style/uncrustify-changed.sh @@ -0,0 +1,37 @@ +#!/bin/bash +# +# This file checks your git index and runs indent on every changed *.c and *.h +# file. +# +# Author: Valentin Sawadski + +# Exit if any called command exits nonzero +set -e + +# Determine the repository root as the path returned by porcelain always +# start there +GIT_REPOSITORY_ROOT=$(git rev-parse --show-toplevel) + +# This is the format command that will be run. +FORMAT_COMMAND=$GIT_REPOSITORY_ROOT/tools/code-style/uncrustify-fix-style.sh + +# Parse all the files we get from git. +RAW_FILES=$(git status --porcelain) +# Set \n to be the field separator +IFS=$'\n' +# This splits the input by newline +GIT_FILES_BY_LINE=( $RAW_FILES ) + +# Now go throug them once more and remove the GIT status logs. +# Count the amount of C and H files we have to format. +IFS=$' ' +for i in "${!GIT_FILES_BY_LINE[@]}"; do + LINE_BY_SPACE=( ${GIT_FILES_BY_LINE[i]} ) + FILE=${LINE_BY_SPACE[1]} + + if [[ ${FILE: -2} = ".c" || ${FILE: -2} = ".h" ]]; then + # echo Formatting File: $FILE + $FORMAT_COMMAND $GIT_REPOSITORY_ROOT/$FILE + fi +done +