Skip to content

Commit 7573423

Browse files
authored
Add Last-Translators to TX pull commit message (#263)
1 parent 06403be commit 7573423

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
polib==1.2.0
12
pomerge==0.1.4
23
potodo==0.23.1
34
powrap==1.0.2

scripts/commit.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
set -eu
77

8-
cd $(dirname $0)/../cpython/Doc/locales/${PYDOC_LANGUAGE}/LC_MESSAGES
8+
rootdir=$(realpath $(dirname $0))
9+
cd $rootdir/../cpython/Doc/locales/${PYDOC_LANGUAGE}/LC_MESSAGES
910

1011
extra_files=".tx/config stats.json potodo.md"
1112

@@ -41,4 +42,7 @@ fi
4142
set -u
4243

4344
# Commit only if there is any cached file
44-
git diff-index --cached --quiet HEAD || { git add -v $extra_files; git commit -vm "Update translations"; }
45+
if ! git diff-index --cached --quiet HEAD; then
46+
git add -v $extra_files
47+
git commit -vm "$($rootdir/generate_commit_msg.py)"
48+
fi

scripts/generate_commit_msg.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python
2+
"""
3+
Generate a commit message
4+
Parses staged files and generates a commit message with Last-Translator's as
5+
co-authors.
6+
Based on Stan Ulbrych's implementation for Python Doc' Polish team
7+
"""
8+
9+
import argparse
10+
import contextlib
11+
import os
12+
from subprocess import run, CalledProcessError
13+
from pathlib import Path
14+
15+
from polib import pofile, POFile
16+
17+
18+
def generate_commit_msg():
19+
translators: set[str] = set()
20+
21+
result = run(
22+
["git", "diff", "--cached", "--name-only", "--diff-filter=ACM"],
23+
capture_output=True,
24+
text=True,
25+
check=True,
26+
)
27+
staged = [
28+
filename for filename in result.stdout.splitlines() if filename.endswith(".po")
29+
]
30+
31+
for file in staged:
32+
staged_file = run(
33+
["git", "show", f":{file}"], capture_output=True, text=True, check=True
34+
).stdout
35+
try:
36+
old_file = run(
37+
["git", "show", f"HEAD:{file}"],
38+
capture_output=True,
39+
text=True,
40+
check=True,
41+
).stdout
42+
except CalledProcessError:
43+
old_file = ""
44+
45+
new_po = pofile(staged_file)
46+
old_po = pofile(old_file) if old_file else POFile()
47+
old_entries = {entry.msgid: entry.msgstr for entry in old_po}
48+
49+
for entry in new_po:
50+
if entry.msgstr and (
51+
entry.msgid not in old_entries
52+
or old_entries[entry.msgid] != entry.msgstr
53+
):
54+
translator = new_po.metadata.get("Last-Translator")
55+
translator = translator.split(",")[0].strip()
56+
if translator:
57+
translators.add(f"Co-Authored-By: {translator}")
58+
break
59+
60+
print("Update translation\n\n" + "\n".join(translators))
61+
62+
63+
# contextlib implemented chdir since Python 3.11
64+
@contextlib.contextmanager
65+
def chdir(path: Path):
66+
"""Temporarily change the working directory."""
67+
original_dir = Path.cwd()
68+
os.chdir(path)
69+
try:
70+
yield
71+
finally:
72+
os.chdir(original_dir)
73+
74+
75+
if __name__ == "__main__":
76+
parser = argparse.ArgumentParser(
77+
description="Generate commit message with translators as co-authors."
78+
)
79+
parser.add_argument(
80+
"path",
81+
type=Path,
82+
nargs='?',
83+
default=".",
84+
help="Path to the Git repository (default: current directory)",
85+
)
86+
args = parser.parse_args()
87+
88+
with chdir(args.path):
89+
generate_commit_msg()

0 commit comments

Comments
 (0)