git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Maintaince script for l10n files and commits

From
Jiang Xin <worldhello.net@gmail.com>
Date
Mar 8, 2012, 16:05 UTC
Message-ID
<1331222706-81728-1-git-send-email-worldhello.net@gmail.com>
In-Reply-To
<7vr4x4mdae.fsf@alter.siamese.dyndns.org>
Usage of this script:
 * po-helper.sh XX.po   : Init or update XX.po from git.pot
 * po-helper.sh check [XX.po]
                        : Perform all the checks for XX.po
 * po-helper.sh commits [since] [til]
                        : Check non-ascii chars in commit logs
                          Don't write commit log with non-ascii chars
                          without proper encoding settings.
                          Subject of commit log must written in English.
                          Don't change files outside this directory (po/)
 * po-helper.sh pot     : Display summary of updates of git.pot file
Signed-off-by: Jiang Xin <worldhello.net@gmail.com>
---
 po/po-helper.sh |  271 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 271 insertions(+)
 create mode 100755 po/po-helper.sh
diff --git a/po/po-helper.sh b/po/po-helper.sh
new file mode 100755
index 0000000..dd370a5
--- /dev/null
+++ b/po/po-helper.sh
@@ -0,0 +1,271 @@
+#!/bin/bash
+#
+# Copyright (c) 2012 Jiang Xin
+
+POTFILE=git.pot
+
+usage()
+{
+    cat <<-END_OF_USAGE
+Maintaince script for l10n files and commits.
+
+Usage:
+
+ * po-helper.sh XX.po   : Init or update XX.po from git.pot
+
+ * po-helper.sh check [XX.po]
+                        : Perform all the checks for XX.po
+
+ * po-helper.sh commits [since] [til]
+                        : Check non-ascii chars in commit logs
+
+                          Don't write commit log with non-ascii chars
+                          without proper encoding settings.
+
+                          Subject of commit log must written in English.
+
+                          Don't change files outside this directory (po/)
+
+ * po-helper.sh pot     : Display summary of updates of git.pot file
+
+END_OF_USAGE
+
+    exit 1
+}
+
+# Display summary of updates of git.pot
+show_pot_update_summary()
+{
+    pnew="^.*:([0-9]+): this message is used but not defined in"
+    pdel="^.*:([0-9]+): warning: this message is not used"
+    new_count=0
+    del_count=0
+    new_lineno=""
+    del_lineno=""
+
+    status=$(git status --porcelain -- $POTFILE)
+    if [ -z "$status" ]; then
+        echo "Nothing changed."
+    else
+        tmpfile=$(mktemp /tmp/git.po.XXXX)
+        LANGUAGE=C git show HEAD:./git.pot > $tmpfile
+        LANGUAGE=C msgcmp -N --use-untranslated $tmpfile $POTFILE 2>&1 |
+        {    while read line; do
+                if [[ $line =~ $pnew ]]; then
+                    new_count=$(( new_count + 1 ))
+                    if [ -z "$new_lineno" ]; then
+                        new_lineno="${BASH_REMATCH[1]}"
+                    else
+                        new_lineno="${new_lineno}, ${BASH_REMATCH[1]}"
+                    fi
+                fi
+                if [[ $line =~ $pdel ]]; then
+                    del_count=$(( del_count + 1 ))
+                    if [ -z "$del_lineno" ]; then
+                        del_lineno="${BASH_REMATCH[1]}"
+                    else
+                        del_lineno="${del_lineno}, ${BASH_REMATCH[1]}"
+                    fi
+                fi
+            done
+            [ $new_count -gt 1 ] && new_plur="s" || new_plur=""
+            [ $del_count -gt 1 ] && del_plur="s" || del_plur=""
+            echo "Updates of $POTFILE since last update:"
+            echo
+            echo " * Add ${new_count} new l10n message${new_plur}" \
+                 "in the new generated \"git.pot\" file at" \
+                 "line${new_plur}:"
+            echo "   ${new_lineno}"
+            echo
+
+            echo " * Remove ${del_count} l10n message${del_plur}" \
+                 "from the old \"git.pot\" file at line${del_plur}:"
+            echo "   ${del_lineno}"
+        }
+        rm $tmpfile
+    fi
+}
+
+# Syntax check on XX.po, or all .po files if nothing provided
+check_po()
+{
+    if [ $# -eq 0 ]; then
+        ls *.po | while read f; do
+            echo "============================================================"
+            echo "Check $f..."
+            check_po $f
+        done
+    fi
+    while [ $# -gt 0 ]; do
+        po=$1
+        shift
+        if [ -f $po ]; then
+            msgfmt -o /dev/null --check --statistics $po
+        else
+            echo "Error: File $po does not exist." >&2
+        fi
+    done
+}
+
+# Create or update XX.po file from git.pot
+create_or_update_po()
+{
+    if [ $# -eq 0 ]; then
+        usage
+    fi
+    while [ $# -gt 0 ]; do
+        po=$1
+        shift
+        if [ -f $po ]; then
+            msgmerge --add-location --backup=off -U $po $POTFILE
+        else
+            msginit -i $POTFILE --locale=${po%.po}
+            perl -pi -e 's/(?<="Project-Id-Version: )PACKAGE VERSION/Git/' $po
+        fi
+        mo="build/locale/${po%.po}/LC_MESSAGES/git.mo"
+        mkdir -p $(dirname $mo)
+        msgfmt -o $mo --check --statistics $po
+    done
+}
+
+
+verify_commit_encoding()
+{
+    c=$1
+    subject=0
+    non_ascii=""
+    encoding=""
+    log=""
+
+    git cat-file commit $c |
+    {
+        while read line; do
+            log="$log - $line"
+            # next line would be the commit log subject line,
+            # if no previous empty line found.
+            if [ -z "$line" ]; then
+                subject=$((subject + 1))
+                continue
+            fi
+            pencoding="^encoding (.+)"
+            if [ $subject -eq 0 ] && [[ $line =~ $pencoding ]]; then
+                encoding="${BASH_REMATCH[1]}"
+            fi
+            # non-ascii found in commit log
+            pnonascii="([^[:alnum:][:space:][:punct:]]+)"
+            if [[ $line =~ $pnonascii ]]; then
+                non_ascii="${BASH_REMATCH[1]} >> $line <<"
+                if [ $subject -eq 1 ]; then
+                    report_nonascii_in_subject $c "$non_ascii"
+                    return
+                fi
+            fi
+            # subject has only one line
+            [ $subject -eq 1 ] && subject=$((subject += 1))
+            # break if there are non-asciis and has already checked subject line
+            if [ -n "$non_ascii" ] && [ $subject -gt 0 ]; then
+                break
+            fi
+        done
+        if [ -n "$non_ascii" ]; then
+            if [ -z "$encoding" ]; then
+                echo $line | iconv -f UTF-8 -t UTF-8 -s >/dev/null ||
+                report_bad_encoding "$c" "$non_ascii"
+            else
+                echo $line | iconv -f $encoding -t UTF-8 -s >/dev/null ||
+                report_bad_encoding "$c" "$non_ascii" "$encoding"
+            fi
+        fi
+    }
+}
+
+report_nonascii_in_subject()
+{
+    c=$1
+    non_ascii=$2
+
+    echo "============================================================" >&2
+    echo "Error: Non-ASCII in subject of commit $c:"                    >&2
+    echo "       ${non_ascii}"                                          >&2
+    echo
+    git cat-file commit $c | head -15 | while read line; do
+        echo "\t$line"                                                  >&2
+    done
+}
+
+report_bad_encoding()
+{
+    c=$1
+    non_ascii=$2
+    encoding=$3
+
+    echo "============================================================" >&2
+    if [ -z "$encoding" ]; then
+        echo "Error: Not have encoding setting for commit $c:"          >&2
+    else
+        echo "Error: Wrong encoding ($encoding) for commit $c:"         >&2
+    fi
+    echo "       ${non_ascii}"                                          >&2
+    echo                                                                >&2
+    git cat-file commit $c | head -15 | while read line; do
+        echo "\t$line"                                                  >&2
+    done
+}
+
+# Check commit logs for bad encoding settings
+check_commits()
+{
+    if [ $# -gt 2 ]; then
+        usage
+    fi
+    since=${1:-origin/master}
+    til=${2:-HEAD}
+
+    git diff-tree -r $since $til | awk '{print $6}' |
+    while read f; do
+        if [[ ! $f =~ ^po/ ]]; then
+            echo "Error: changed files ($f...) outside po directory!"   >&2
+            echo "       reference: git diff-tree -r $since $til"       >&2
+        fi
+    done
+
+    count=0
+    git rev-list ${since}..${til} |
+    {   while read c; do
+            verify_commit_encoding $c
+            count=$(( count + 1 ))
+        done
+        echo
+        echo "$count commits check complete."
+    }
+}
+
+
+[ $# -eq 0 ] && usage
+
+while [ $# -ne 0 ]; do
+    case "$1" in
+    pot|git.pot)
+        show_pot_update_summary
+        ;;
+    *.po)
+        create_or_update_po $1
+        ;;
+    check)
+        shift
+        check_po $*
+        exit 0
+        ;;
+    commit|commits)
+        shift
+        check_commits $*
+        exit 0
+        ;;
+    *)
+        usage
+        ;;
+    esac
+    shift
+done
+
+# vim: et ts=4 sw=4
-- 
1.7.9.2.330.gaa956.dirty
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 10 in “Maintaince script for l10n files and commits”
  1. Maintaince script for l10n files and commitsJiang Xin, Mar 7, 2012
  2. Junio C HamanoMar 7, 2012
  3. Maintaince script for l10n files and commitsJiang Xin, Mar 8, 2012
  4. Junio C HamanoMar 8, 2012
  5. Jiang XinMar 9, 2012
  6. Maintaince script for l10n files and commitsJiang Xin, Mar 9, 2012
  7. David AguilarMar 9, 2012
  8. Jiang XinMar 9, 2012
  9. Junio C HamanoMar 10, 2012
  10. Maintaince script for l10n files and commitsJiang Xin, Mar 10, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.