From 81c61b672ec0d961c36900695647dc0a30a9f0fd Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Tue, 18 Aug 2020 15:18:22 -0400
Subject: [PATCH] misc: add clang-formatting pre-commit install for cpp
 submodules

Running './make-ring.py --init' will install a pre-commit hook
to run clang-format on the daemon/lrc/client-qt submodules.

Change-Id: Id1a03bf8915f17a99271ab98aa728ce035bc3ffb
---
 make-ring.py      |  7 ++++
 scripts/format.sh | 86 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+)
 create mode 100755 scripts/format.sh

diff --git a/make-ring.py b/make-ring.py
index 3d6629f8..4c09b0da 100755
--- a/make-ring.py
+++ b/make-ring.py
@@ -282,6 +282,13 @@ def run_init():
     for name in module_names:
         copy_file("./scripts/commit-msg", ".git/modules/"+name+"/hooks")
 
+    module_names_to_format = ['daemon', 'lrc', 'client-qt']
+    for name in module_names_to_format:
+        execute_script(
+            ['./scripts/format.sh --install  %(path)s'],
+            {"path": ".git/modules/" + name + "/hooks"}
+        )
+
 
 def copy_file(src, dest):
     print("Copying:" + src + " to " + dest)
diff --git a/scripts/format.sh b/scripts/format.sh
new file mode 100755
index 00000000..ebd3774a
--- /dev/null
+++ b/scripts/format.sh
@@ -0,0 +1,86 @@
+#!/usr/bin/env bash
+
+set -e
+
+command_exists ()
+{
+    type "$1" &> /dev/null ;
+}
+
+CFVERSION="9"
+CLANGFORMAT=""
+if command_exists clang-format-${CFVERSION}; then
+    CLANGFORMAT=clang-format-${CFVERSION}
+else
+    if command_exists clang-format; then
+        CLANGFORMAT=clang-format
+    fi
+fi
+
+if ! command -v $CLANGFORMAT &> /dev/null; then
+    echo "Required version of clang-format not found"
+    exit 1
+fi
+
+format_file()
+{
+    if [ -f "${1}" ]; then
+        $CLANGFORMAT -i -style=file "${1}" || true
+    fi
+}
+
+format_files()
+{
+    for file in $1; do
+        echo -ne "Formatting: ${file}\\033[0K\\r"
+        format_file "${file}"
+    done
+}
+
+exit_if_no_files()
+{
+    echo No files to format
+    exit 0
+}
+
+install_hook()
+{
+    hooks_path=$1
+    if [ ! -d "$hooks_path" ]; then
+        echo "$hooks_path" path does not exist
+        exit 1
+    fi
+    echo Installing pre-commit hook in "$hooks_path"
+    echo "/bin/bash $(realpath $0)" > "$hooks_path"/pre-commit
+    chmod +x "$hooks_path"/pre-commit
+}
+
+display_help()
+{
+    echo "Usage: $0 [OPTION...] -- Clang format source files with a .clang-format file" >&2
+    echo
+    echo "   --all             format all files instead of only committed ones"
+    echo "   --install <path>  install a pre-commit hook to run this script"
+    echo
+}
+
+if [ "$1" == "--help" ]; then
+    display_help
+    exit 0
+fi
+
+case "${1}" in
+  --all )
+    files=$(find src -regex '.*\.\(cpp\|hpp\|cc\|cxx\|h\)') || true
+    echo Formatting all source files...
+    format_files "$files"
+    ;;
+  --install )
+    install_hook "${2}"
+    ;;
+  * )
+    files=$(git diff-index --cached --name-only HEAD | grep -iE '\.(cpp|cxx|cc|h|hpp)') || exit_if_no_files
+    echo Formatting committed source files...
+    format_files "$files"
+    ;;
+esac
-- 
GitLab