From 1e0200fa309be35c1a3ade9dca36af65ca5bd179 Mon Sep 17 00:00:00 2001
From: Hans-Christoph Steiner <hans@eds.org>
Date: Wed, 31 Jan 2018 15:19:37 +0100
Subject: [PATCH] add standard pre-commit hook that does quick checks, e.g.
 l18n scripts

---
 hooks/install-hooks.sh                        | 30 +++++++++++
 hooks/pre-commit                              | 52 +++++++++++++++++++
 tools/fix-ellipsis.sh                         |  6 ++-
 tools/remove-unused-and-blank-translations.py |  6 +++
 4 files changed, 93 insertions(+), 1 deletion(-)
 create mode 100755 hooks/install-hooks.sh
 create mode 100755 hooks/pre-commit

diff --git a/hooks/install-hooks.sh b/hooks/install-hooks.sh
new file mode 100755
index 000000000..fd7ff128f
--- /dev/null
+++ b/hooks/install-hooks.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+#
+# Install all the client hooks
+
+BASE_DIR="$(cd $(dirname $0); pwd -P)"
+HOOK_NAMES="applypatch-msg pre-applypatch post-applypatch pre-commit prepare-commit-msg commit-msg post-commit pre-rebase post-checkout post-merge pre-push post-push pre-receive update post-receive post-update pre-auto-gc"
+HOOK_DIR="$(git rev-parse --show-toplevel)/.git/hooks"
+
+for hook in $HOOK_NAMES; do
+
+	shipped_hook="$BASE_DIR/$hook"
+	installed_hook="$HOOK_DIR/$hook"
+
+	# If we don't distribute it, continue
+	if [ ! -f "$shipped_hook" ]; then
+		continue
+	fi
+
+	if [ -h "$installed_hook" ]; then
+		echo "$installed_hook is a symlink - replacing."
+	elif [ -e "$installed_hook" ]; then
+		echo "$installed_hook hook already exists."
+		continue
+	fi
+
+	# Create the symlink
+	echo "ln -s -f \"$shipped_hook\" \"$installed_hook\""
+	ln -s -f "$shipped_hook" "$installed_hook"
+
+done
diff --git a/hooks/pre-commit b/hooks/pre-commit
new file mode 100755
index 000000000..dd3fd78f7
--- /dev/null
+++ b/hooks/pre-commit
@@ -0,0 +1,52 @@
+#!/bin/sh
+#
+# An hook script to verify what is about to be committed.
+# Called by "git commit" with no arguments.  The hook will
+# exit with non-zero status after issuing an appropriate message if
+# it wants to stop the 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
+
+# If you want to allow non-ascii filenames set this variable to true.
+allownonascii=$(git config hooks.allownonascii)
+
+# Redirect output to stderr.
+exec 1>&2
+
+# Cross platform projects tend to avoid non-ascii filenames; prevent
+# them from being added to the repository. We exploit the fact that the
+# printable range starts at the space character and ends with tilde.
+if [ "$allownonascii" != "true" ] &&
+	# Note that the use of brackets around a tr range is ok here, (it's
+	# even required, for portability to Solaris 10's /usr/bin/tr), since
+	# the square bracket bytes happen to fall in the designated range.
+	test $(git diff --cached --name-only --diff-filter=A -z $against |
+	  LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
+then
+	echo "Error: Attempt to add a non-ascii file name."
+	echo
+	echo "This can cause problems if you want to work"
+	echo "with people on other platforms."
+	echo
+	echo "To be portable it is advisable to rename the file ..."
+	echo
+	echo "If you know what you are doing you can disable this"
+	echo "check using:"
+	echo
+	echo "  git config hooks.allownonascii true"
+	echo
+	exit 1
+fi
+
+exec ./tools/fix-ellipsis.sh
+exec ./tools/check-format-strings.py
+exec ./tools/remove-unused-and-blank-translations.py
+
+# If there are whitespace errors, print the offending file names and fail.
+exec git diff-index --check --cached $against --
diff --git a/tools/fix-ellipsis.sh b/tools/fix-ellipsis.sh
index 492e5a538..ed6316d72 100755
--- a/tools/fix-ellipsis.sh
+++ b/tools/fix-ellipsis.sh
@@ -1,5 +1,9 @@
-#!/bin/bash -x
+#!/bin/bash
 
 # Fix TypographyEllipsis programmatically
 
 sed -i 's/\.\.\./…/g' app/src/main/res/values*/*.xml
+if git diff | grep -Eo '^\+.*…'; then
+    echo Fix TypographyEllipsis
+    exit 1
+fi
diff --git a/tools/remove-unused-and-blank-translations.py b/tools/remove-unused-and-blank-translations.py
index 1da2158e4..e46fd98f5 100755
--- a/tools/remove-unused-and-blank-translations.py
+++ b/tools/remove-unused-and-blank-translations.py
@@ -7,8 +7,11 @@
 import glob
 import os
 import re
+import sys
 from xml.etree import ElementTree
 
+count = 0
+
 resdir = os.path.join(os.path.dirname(__file__), '..', 'app', 'src', 'main', 'res')
 sourcepath = os.path.join(resdir, 'values', 'strings.xml')
 
@@ -60,6 +63,7 @@ for d in sorted(glob.glob(os.path.join(resdir, 'values-*'))):
                 found_other = True
         if not found_other:
             print(os.path.relpath(str_path) + ': Missing "other" string in', e.attrib['name'])
+            count += 1
 
     result = re.sub(r' />', r'/>', ElementTree.tostring(root, encoding='utf-8').decode('utf-8'))
 
@@ -67,3 +71,5 @@ for d in sorted(glob.glob(os.path.join(resdir, 'values-*'))):
         f.write(header)
         f.write(result)
         f.write('\n')
+
+sys.exit(count)