From afa43e8b73bde1c79354f4f26f19900e5ad254c4 Mon Sep 17 00:00:00 2001 From: Lee Killough Date: Thu, 10 Oct 2019 19:42:01 -0400 Subject: [PATCH] Handle spaces and newline --- .githooks/pre-commit | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 637d11d5e..fd241bdcc 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,30 +1,24 @@ -#!/bin/sh +#!/bin/bash # # This pre-commit hook checks if any versions of clang-format # are installed, and if so, uses the installed version to format # the staged changes. -base=/opt/rocm/hcc/bin/clang-format -format="" +format=/opt/rocm/hcc/bin/clang-format -# Redirect output to stderr. -exec 1>&2 +# Redirect stdout to stderr. +exec >&2 - # check if clang-format is installed -type "$base" >/dev/null 2>&1 && format="$base" - -# no versions of clang-format are installed -if [ -z "$format" ] -then - echo "$base is not installed. Pre-commit hook will not be executed." +# check if clang-format is installed +if [[ ! -x $format ]]; then + echo "$format is not installed. Pre-commit hook will not be executed." exit 0 fi # Do everything from top - level cd $(git rev-parse --show-toplevel) -if git rev-parse --verify HEAD >/dev/null 2>&1 -then +if git rev-parse --verify HEAD >/dev/null 2>&1; then against=HEAD else # Initial commit: diff against an empty tree object @@ -39,10 +33,10 @@ for file in $(git diff-index --cached --name-only $against); do done # do the formatting -for file in $(git diff-index --cached --name-only $against | grep -E '\.h$|\.hpp$|\.cpp$|\.cl$|\.h\.in$|\.hpp\.in$|\.cpp\.in$') -do - if [ -e "$file" ] - then +for file in $(git diff-index --cached --name-only $against | grep -E '\.h$|\.hpp$|\.cpp$|\.cl$|\.h\.in$|\.hpp\.in$|\.cpp\.in$'); do + if [[ -e "$file" ]]; then + sed -i -e 's/[[:space:]]*$//' "$file" # Remove whitespace at end of lines + sed -i -e '$a\' "$file" # Add missing newline to end of file echo "$format $file" "$format" -i -style=file "$file" fi