Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MC: allow compile optimizations in src/kernel/lmm/.
[simgrid.git] / tools / git-hooks / clang-format.pre-commit
index ae66d78..8d1bd55 100755 (executable)
 ##################################################################
 # SETTINGS
 # set path to clang-format binary
-CLANG_FORMAT=$(which clang-format)
+CLANG_FORMAT=$(which clang-format-3.8)
+
+# git subcommand that we want to us
+for name in git-clang-format-3.9 git-clang-format-3.8 git-clang-format ; do
+ where=$(which $name)
+ if [ x != "x$where" ] ; then
+   GIT_SUBCOMMAND=$(echo $name|sed 's/git-//')
+   break
+ fi
+done
+if [ -z $GIT_SUBCOMMAND ] ; then
+  echo "Cannot find git-clang-format (in version >=3.8). Did you install the right package?"
+  exit 1
+fi
 
 # remove any older patches from previous commits. Set to true or false.
-DELETE_OLD_PATCHES=false
+DELETE_OLD_PATCHES=true
 
 # 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 clang-format.
@@ -61,10 +74,10 @@ canonicalize_filename () {
     local result=""
 
     # Need to restore the working directory after work.
-    pushd `pwd` > /dev/null
+    pushd $(pwd) > /dev/null
 
     cd "$(dirname "$target_file")"
-    target_file=`basename $target_file`
+    target_file=$(basename $target_file)
 
     # Iterate down a (possible) chain of symlinks
     while [ -L "$target_file" ]
@@ -76,7 +89,7 @@ canonicalize_filename () {
 
     # Compute the canonicalized name by finding the physical path
     # for the directory we're in and appending the target file.
-    physical_directory=`pwd -P`
+    physical_directory=$(pwd -P)
     result="$physical_directory"/"$target_file"
 
     # restore the working directory after work.
@@ -108,7 +121,7 @@ else
 fi
 
 if [ ! -x "$CLANG_FORMAT" ] ; then
-    printf "Error: clang-format executable not found.\n"
+    printf "Error: clang-format executable not found. sudo apt-get install clang-format-3.8\n"
     printf "Set the correct path in $(canonicalize_filename "$0").\n"
     exit 1
 fi
@@ -136,7 +149,9 @@ do
     #    +++ - 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'.
-    git clang-format --diff -q >> "$patch"
+    git ${GIT_SUBCOMMAND} --binary ${CLANG_FORMAT} --diff -q "$file" \
+      | (grep -v "no modified files to format"||true) \
+      >> "$patch"
     #"$CLANG_FORMAT" -style=file "$file" | \
         #diff -u "$file" - | \
         #sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch"
@@ -154,12 +169,11 @@ printf "\nThe following differences were found between the code to commit "
 printf "and the clang-format 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 "\nNote: git apply only modifies your working directory, but not your index, i.e., not what you want to commit.\n"
-printf "You need to run 'git add' again after you applied this patch OR just run git apply a second time:"
-printf "\n git apply --cached $patch\n\n"
-printf "Aborting commit. Apply changes and commit again or skip checking with"
-printf " --no-verify (not recommended).\n"
+printf "\nYou can apply these changes and readd the files with:\n"
+printf "  git apply $patch &&  git apply --cached $patch\n"
+printf "(call this command from the root directory of your repository)\n"
+printf "\n\n\n"
+printf "Aborting commit. Apply changes and commit again.\n"
+# printf "Skip checking with: --no-verify (not recommended).\n"
 
 exit 1