Browse Source

improve syntax check hook, and add check for outdated translations

master
Max Mehl 1 month ago
parent
commit
c9f72347d5
Signed by: max.mehl <max.mehl@fsfe.org> GPG Key ID: 2704E4AB371E2E92
1 changed files with 73 additions and 5 deletions
  1. 73
    5
      tools/githooks/pre-commit

+ 73
- 5
tools/githooks/pre-commit View File

@@ -1,13 +1,81 @@
1
-#!/bin/sh
1
+#!/usr/bin/env sh
2 2
 
3
-TESTS=0
3
+GIT_ROOT=$(git rev-parse --show-toplevel)
4 4
 
5
+# CHECK XML SYNTAX
6
+TESTS=0
5 7
 for xmlfile in $(
6
-  git status -s \
7
-  | sed -En "s/\s*[AM]\s*(.*\.xsl|.*\.xml|.*\.xhtml)/\1/p"
8
+  git diff --staged --name-only | grep -E "(\.xhtml$|\.xml$|\.xsl$)"
8 9
 ); do
9 10
   xmllint --noout --nonet "${xmlfile}"
10 11
   TESTS=$((${TESTS} + $?))
11 12
 done
12 13
 
13
-exit "$TESTS"
14
+if [ $TESTS -gt 0 ]; then
15
+
16
+cat <<EOF >&2
17
+
18
+  === SYNTAX ERROR ===
19
+  One or more files failed the XML syntax check!
20
+
21
+  The error log above will help you to identify the error. Read it from
22
+  the top. The numbers behind the file name point to the line number in
23
+  the file where the error has been detected.
24
+
25
+  Check this line and its surroundings for XML/HTML tags that have not 
26
+  been closed correctly, errors with special characters like "&", or
27
+  other syntactical mistakes.
28
+
29
+  The commit has been aborted. You have to fix the problem first.
30
+EOF
31
+
32
+  exit $TESTS
33
+fi
34
+
35
+
36
+# CHECK TRANSLATIONS WHICH WILL BE OUTDATED
37
+ALLFILES=$(git diff --staged --name-only | grep -E "(\.xhtml$|\.xml$)")
38
+ENFILES=$(echo "${ALLFILES}" | grep -E "\.en\.")
39
+OUTDATED=0
40
+TEXT=
41
+
42
+#echo "$ALLFILES"
43
+
44
+for file in $ENFILES; do
45
+  WARN=
46
+  #echo $file
47
+  # Get file extension
48
+  EXT="${file##*.}"
49
+  # remove "en.$EXT"
50
+  BASE=$(echo "${file}" | sed -E "s/\.[a-z][a-z]\.$EXT//")
51
+  LANGS_UP=$("${GIT_ROOT}"/tools/check-translation-status.sh -f "${file}" -o up)
52
+  #echo $BASE $LANGS_UP
53
+
54
+  # check whether previously up-to-date translations will be in this commit
55
+  for trans in $LANGS_UP; do
56
+    trans="$BASE.$trans.$EXT"
57
+    if ! $(echo "${ALLFILES}" | grep -Eq "^${trans}$" ); then
58
+      OUTDATED=$((${OUTDATED} + 1))
59
+      WARN="$WARN, $trans"
60
+    fi
61
+  done
62
+  WARN=$(echo $WARN | sed -E "s/^, //")
63
+  TEXT="${TEXT}|$file => $WARN"
64
+done
65
+
66
+if [ $OUTDATED -gt 0 ]; then
67
+  cat <<EOF >&2
68
+  === OUTDATE WARNING ===
69
+  Your commit caused $OUTDATED previously up-to-date translations 
70
+  to become outdated!
71
+
72
+  If you made non-content changes to the English base files, 
73
+  consider making a fake-commit to these translations:
74
+
75
+  - $(echo $TEXT | sed -E -e "s/^\|//" -e "s/\|/\n  - /g")
76
+
77
+  Read more: https://wiki.fsfe.org/TechDocs/Mainpage/Translations/Outdated
78
+
79
+EOF
80
+
81
+fi

Loading…
Cancel
Save