Browse Source

Merge PR #1091 into master

master
Max Mehl 1 week ago
parent
commit
ad938471eb
2 changed files with 105 additions and 49 deletions
  1. 45
    36
      tools/check-translation-status.sh
  2. 60
    13
      tools/githooks/pre-commit

+ 45
- 36
tools/check-translation-status.sh View File

@@ -21,109 +21,118 @@ print_usage() {
echo " 0 = file is up-to-date"
echo " 1 = file is outdated"
echo " 2 = file is not supported"
exit 0
}

ALL="0"
QUIET="0"
while getopts f:o:aq OPT; do
while getopts f:o:aqh OPT; do
case $OPT in
f) FILE=$OPTARG;; # file name
a) ALL="1";;
q) QUIET="1";;
o) ONLY=$OPTARG;; # print only languages which are up/outdated
h) print_help;;
f) FILE=$OPTARG;; # file name
a) ALL="1";;
q) QUIET="1";;
o) ONLY=$OPTARG;; # print only languages which are up/outdated
h) print_usage;;
*) print_usage;;
esac
done

# show usage if no required parameter given
if [ "$1" = "" ] || [ -z "$FILE" ]; then
if [ "$1" = "" ] || [ -z "${FILE}" ]; then
print_usage
exit 0
fi

# -o suppresses all other output (-q), and implicates -a
if [ ! -z "$ONLY" ]; then
if [ -n "${ONLY}" ]; then
QUIET="1"
ALL="1"
fi

function out {
if [ "$QUIET" != "1" ]; then
if [ "${QUIET}" != "1" ]; then
echo "$@"
fi
}

# Get file extension
EXT="${FILE##*.}"
if ! [[ "$EXT" =~ ^(xhtml|xml)$ ]]; then
if ! [[ "${EXT}" =~ ^(xhtml|xml)$ ]]; then
out "Only works for files ending with .xhtml or .xml"
exit 2
fi

# remove "en.$EXT"
BASE=$(echo "$FILE" | sed -E "s/\.[a-z][a-z]\.$EXT//")
BASE=$(echo "${FILE}" | sed -E "s/\.[a-z][a-z]\.${EXT}//")
# get change date of English file
EN=$BASE.en.$EXT
if [ ! -e "$EN" ]; then
out "English file does not exist. Aborting. ($EN)"
EN="${BASE}".en."${EXT}"
if [ ! -e "${EN}" ]; then
out "English file does not exist. Aborting. (${EN})"
exit 2
fi
endate=$(git log --pretty="%cd" --date=raw -1 "$EN"|cut -d' ' -f1)
endate=$(git log --pretty="%cd" --date=raw -1 "${EN}"|cut -d' ' -f1)

# check if we have a git log of the EN base file. If not, abort
if [ -z "${endate}" ]; then
out "EN file does not have any Git log. Ususally this means the file is new"
exit 0
fi

# Convert to YYYY-MM-DD
ymd=$(date +"%Y-%m-%d" --date="@$endate")
out "Basefile: $EN ( $ymd )"
out "Basefile: ${EN} ( ${ymd} )"
out " STATUS LANG DATE"
out " -------- ---- ----------"

if [ "$ALL" == "1" ]; then
if [ "${ALL}" == "1" ]; then
# Loop over all translations of this file
for i in "$BASE".[a-z][a-z].$EXT; do
for i in "${BASE}".[a-z][a-z]."${EXT}"; do
if [[ $i != *".en."* ]]; then
# get language code
lang=$(echo "$i"|sed "s/.*\.\([a-z][a-z]\)\.$EXT/\1/")
lang=$(echo "${i}"|sed "s/.*\.\([a-z][a-z]\)\.${EXT}/\1/")
# get change date of translation
trdate=$(git log --pretty="%cd" --date=raw -1 "$i"|cut -d' ' -f1)
trdate=$(git log --pretty="%cd" --date=raw -1 "${i}"|cut -d' ' -f1)
# Convert to YYYY-MM-DD
ymd=$(date +"%Y-%m-%d" --date="@$trdate")
ymd=$(date +"%Y-%m-%d" --date="@${trdate}")
# get time difference in seconds
diff=$((trdate-endate))
# mark as outdated if difference larger than 1 hour
if [[ $diff -lt -3600 ]]; then
out " OUTDATED $lang $ymd"
out " OUTDATED ${lang} ${ymd}"
# print outdated language code
if [ "$ONLY" == "out" ]; then
echo "$lang"
if [ "${ONLY}" == "out" ]; then
echo "${lang}"
fi
else
out " Up-to-date $lang $ymd"
out " Up-to-date ${lang} ${ymd}"
# print up-to-date language code
if [ "$ONLY" == "up" ]; then
echo "$lang"
if [ "${ONLY}" == "up" ]; then
echo "${lang}"
fi
fi
fi
done | sort
exit 0
else
i="$FILE"
i="${FILE}"
if [[ $i != *".en."* ]]; then
# get language code
lang=$(echo "$i"|sed "s/.*\.\([a-z][a-z]\)\.$EXT/\1/")
lang=$(echo "${i}"|sed "s/.*\.\([a-z][a-z]\)\.${EXT}/\1/")
# get change date of translation
trdate=$(git log --pretty="%cd" --date=raw -1 "$i"|cut -d' ' -f1)
trdate=$(git log --pretty="%cd" --date=raw -1 "${i}"|cut -d' ' -f1)
# Convert to YYYY-MM-DD
ymd=$(date +"%Y-%m-%d" --date="@$trdate")
ymd=$(date +"%Y-%m-%d" --date="@${trdate}")
# get time difference in seconds
diff=$((trdate-endate))
# mark as outdated if difference larger than 1 hour
if [[ $diff -lt -3600 ]]; then
out " OUTDATED $lang $ymd"
out " OUTDATED ${lang} ${ymd}"
exit 1
else
out " Up-to-date $lang $ymd"
out " Up-to-date ${lang} ${ymd}"
exit 0
fi
else
out " (Comparing status of English file does not make sense)"
fi
else
out " (Comparing status of English file does not make sense)"
fi
fi

+ 60
- 13
tools/githooks/pre-commit View File

@@ -9,7 +9,7 @@ for xmlfile in $(
); do
if [ -e "${xmlfile}" ]; then
xmllint --noout --nonet "${xmlfile}"
TESTS=$((${TESTS} + $?))
TESTS=$((TESTS + $?))
fi
done

@@ -34,6 +34,54 @@ EOF
exit $TESTS
fi

# CHECK FOR NEWLY INTRODUCED TAGS
NEWTAGS=0
FILES=""
# test all newly staged (added) XML files in /news and /events
for xmlfile in $(
git diff --staged --name-only | grep -E "^(news/|events/).*(\.xhtml$|\.xml$|\.xsl$)"
); do
hit=0
tags=""
# go through all tags in this file
for tag in $(grep -Ei "<tag.*?>.+?</tag>" "${xmlfile}" | sed -E "s|.*<tag.*?>(.+?)</tag>.*|\1|g"); do
# check if this tag does exist in any other news/event item
if ! git grep -irlE "<tag.*?>${tag}</tag>" news/ events/ | grep -vq "${xmlfile}"; then
hit=1
tags="${tag}, ${tags}"
NEWTAGS=$((NEWTAGS + 1))
fi
done
# if any new tag has been found, enlist them
if [ $hit != 0 ]; then
tags="${tags%, }"
FILES="${FILES}|${xmlfile} (new tag(s): ${tags})"
fi
done

if [ $NEWTAGS -gt 0 ]; then
cat <<EOF >&2
=== NEW / DUPLICATED TAG(S) ===
Your commit introduced $NEWTAGS tag(s) which did not exist before in
our news or event items!
$(echo "${FILES}" | sed -E -e "s/\|/\n - /g")

Please make sure that you use already used tags, and only introduce a
new tag e.g. if it's about a new campaign that will be more often
mentioned in news or events. If you feel unsure, please ask
<web@lists.fsfe.org>.

Here you will find the currently used tags:
https://fsfe.org/tags/tags.html

Your commit has been executed anyway. Please make another commit to
replace a new tag with an already existing one unless you are really
sure. Thank you.

EOF

fi


# CHECK TRANSLATIONS WHICH WILL BE OUTDATED
ALLFILES=$(git diff --staged --name-only | grep -E "(\.xhtml$|\.xml$)")
@@ -41,40 +89,39 @@ ENFILES=$(echo "${ALLFILES}" | grep -E "\.en\.")
OUTDATED=0
TEXT=

#echo "$ALLFILES"

for file in $ENFILES; do
WARN=
#echo $file
# Get file extension
EXT="${file##*.}"
# remove "en.$EXT"
BASE=$(echo "${file}" | sed -E "s/\.[a-z][a-z]\.$EXT//")
LANGS_UP=$("${GIT_ROOT}"/tools/check-translation-status.sh -f "${file}" -o up)
#echo $BASE $LANGS_UP

# check whether previously up-to-date translations will be in this commit
for trans in $LANGS_UP; do
trans="$BASE.$trans.$EXT"
if ! $(echo "${ALLFILES}" | grep -Eq "^${trans}$" ); then
OUTDATED=$((${OUTDATED} + 1))
WARN="$WARN, $trans"
trans="${BASE}.${trans}.${EXT}"
if ! echo "${ALLFILES}" | grep -Eq "^${trans}$"; then
OUTDATED=$((OUTDATED + 1))
WARN="${WARN}, ${trans}"
fi
done
WARN=$(echo $WARN | sed -E "s/^, //")
TEXT="${TEXT}|$file => $WARN"
WARN=$(echo "${WARN}" | sed -E "s/^, //")
# only add to list of outdated files if at least 1 warning has been found
if [ -n "${WARN}" ]; then
TEXT="${TEXT}|${file} => ${WARN}"
fi
done

if [ $OUTDATED -gt 0 ]; then
cat <<EOF >&2
=== OUTDATE WARNING ===
Your commit caused $OUTDATED previously up-to-date translations
Your commit caused ${OUTDATED} previously up-to-date translations
to become outdated!

If you made non-content changes to the English base files,
consider making a fake-commit to these translations:

- $(echo $TEXT | sed -E -e "s/^\|//" -e "s/\|/\n - /g")
- $(echo "${TEXT}" | sed -E -e "s/^\|//" -e "s/\|/\n - /g")

Read more: https://wiki.fsfe.org/TechDocs/Mainpage/Translations/Outdated


Loading…
Cancel
Save