From 960d8424daed06ec54bcbc9f9cddb9ab41eac76e Mon Sep 17 00:00:00 2001 From: Rafael Fontenelle Date: Mon, 30 Jun 2025 23:42:43 -0300 Subject: [PATCH] Add Last-Translators to TX pull commit message --- requirements.txt | 1 + scripts/commit.sh | 8 ++- scripts/generate_commit_msg.py | 89 ++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100755 scripts/generate_commit_msg.py diff --git a/requirements.txt b/requirements.txt index ab5ca72e8..891663cbb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +polib==1.2.0 pomerge==0.1.4 potodo==0.23.1 powrap==1.0.2 diff --git a/scripts/commit.sh b/scripts/commit.sh index 86c6d8f12..06f265534 100755 --- a/scripts/commit.sh +++ b/scripts/commit.sh @@ -5,7 +5,8 @@ set -eu -cd $(dirname $0)/../cpython/Doc/locales/${PYDOC_LANGUAGE}/LC_MESSAGES +rootdir=$(realpath $(dirname $0)) +cd $rootdir/../cpython/Doc/locales/${PYDOC_LANGUAGE}/LC_MESSAGES extra_files=".tx/config stats.json potodo.md" @@ -41,4 +42,7 @@ fi set -u # Commit only if there is any cached file -git diff-index --cached --quiet HEAD || { git add -v $extra_files; git commit -vm "Update translations"; } +if ! git diff-index --cached --quiet HEAD; then + git add -v $extra_files + git commit -vm "$($rootdir/generate_commit_msg.py)" +fi diff --git a/scripts/generate_commit_msg.py b/scripts/generate_commit_msg.py new file mode 100755 index 000000000..16b0b2a20 --- /dev/null +++ b/scripts/generate_commit_msg.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python +""" +Generate a commit message +Parses staged files and generates a commit message with Last-Translator's as +co-authors. +Based on Stan Ulbrych's implementation for Python Doc' Polish team +""" + +import argparse +import contextlib +import os +from subprocess import run, CalledProcessError +from pathlib import Path + +from polib import pofile, POFile + + +def generate_commit_msg(): + translators: set[str] = set() + + result = run( + ["git", "diff", "--cached", "--name-only", "--diff-filter=ACM"], + capture_output=True, + text=True, + check=True, + ) + staged = [ + filename for filename in result.stdout.splitlines() if filename.endswith(".po") + ] + + for file in staged: + staged_file = run( + ["git", "show", f":{file}"], capture_output=True, text=True, check=True + ).stdout + try: + old_file = run( + ["git", "show", f"HEAD:{file}"], + capture_output=True, + text=True, + check=True, + ).stdout + except CalledProcessError: + old_file = "" + + new_po = pofile(staged_file) + old_po = pofile(old_file) if old_file else POFile() + old_entries = {entry.msgid: entry.msgstr for entry in old_po} + + for entry in new_po: + if entry.msgstr and ( + entry.msgid not in old_entries + or old_entries[entry.msgid] != entry.msgstr + ): + translator = new_po.metadata.get("Last-Translator") + translator = translator.split(",")[0].strip() + if translator: + translators.add(f"Co-Authored-By: {translator}") + break + + print("Update translation\n\n" + "\n".join(translators)) + + +# contextlib implemented chdir since Python 3.11 +@contextlib.contextmanager +def chdir(path: Path): + """Temporarily change the working directory.""" + original_dir = Path.cwd() + os.chdir(path) + try: + yield + finally: + os.chdir(original_dir) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="Generate commit message with translators as co-authors." + ) + parser.add_argument( + "path", + type=Path, + nargs='?', + default=".", + help="Path to the Git repository (default: current directory)", + ) + args = parser.parse_args() + + with chdir(args.path): + generate_commit_msg()