git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jiang Xin <worldhello.net@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: Git List <git@vger.kernel.org>,
	avarab@gmail.com, Jiang Xin <worldhello.net@gmail.com>
Subject: [PATCH] Maintaince script for l10n files and commits
Date: Fri,  9 Mar 2012 00:05:06 +0800	[thread overview]
Message-ID: <1331222706-81728-1-git-send-email-worldhello.net@gmail.com> (raw)
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

  reply	other threads:[~2012-03-08 16:05 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-07 18:47 [PATCH] Maintaince script for l10n files and commits Jiang Xin
2012-03-07 19:17 ` Junio C Hamano
2012-03-08 16:05   ` Jiang Xin [this message]
2012-03-08 20:41     ` Junio C Hamano
2012-03-09  0:57       ` Jiang Xin
2012-03-09  6:08       ` [PATCH v2] " Jiang Xin
2012-03-09  6:20         ` David Aguilar
2012-03-09  6:31           ` Jiang Xin
2012-03-10  0:40         ` Junio C Hamano
2012-03-10  9:17       ` [PATCH v3] " Jiang Xin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1331222706-81728-1-git-send-email-worldhello.net@gmail.com \
    --to=worldhello.net@gmail.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).