Skip to content

Commit 8a6c2dc

Browse files
committed
Add a script that reverts changes in po files when only the time stamp has changed
It helps to create meaningful commits, instead of up to 4100 changed files.
1 parent 76bc7b0 commit 8a6c2dc

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ repos:
1515
types: [rst]
1616
entry: python tools/fixup_gh_wiki_anchors.py
1717
language: python
18+
- id: ignore-pot-creation-date
19+
name: Ignore POT-Creation-Date
20+
entry: python tools/ignore_pot_creation_date.py
21+
language: python
22+
files: ^.*\.po$
1823
- repo: https://github.com/pre-commit/pre-commit-hooks
1924
rev: v3.1.0
2025
hooks:

tools/ignore_pot_creation_date.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import argparse
2+
import logging
3+
import os
4+
import subprocess
5+
import sys
6+
import typing
7+
8+
"""
9+
This script reverts changes in PO files when only the timestamp has changed.
10+
It helps to create meaningful commits by ignoring unnecessary changes.
11+
"""
12+
13+
14+
def get_git_changeset(from_ref, to_ref) -> str:
15+
"""
16+
Constructs the changeset string for `git diff` based on from_ref and
17+
to_ref.
18+
"""
19+
from_ref = (
20+
from_ref
21+
or os.getenv("PRE_COMMIT_FROM_REF")
22+
or os.getenv("PRE_COMMIT_SOURCE")
23+
or "HEAD"
24+
)
25+
to_ref = (
26+
to_ref
27+
or os.getenv("PRE_COMMIT_TO_REF")
28+
or os.getenv("PRE_COMMIT_ORIGIN")
29+
)
30+
return f"{from_ref}...{to_ref}" if to_ref else from_ref
31+
32+
33+
def count_meaningful_changes(changeset, po_file) -> int:
34+
"""
35+
Counts meaningful changes in the diff for a given PO file.
36+
"""
37+
cmd = ["git", "diff", "--patch", "--unified=0", changeset, "--", po_file]
38+
proc = subprocess.run(cmd, capture_output=True, text=True)
39+
proc.check_returncode()
40+
41+
diff_lines = proc.stdout.splitlines()
42+
return sum(
43+
1
44+
for line in diff_lines
45+
if (line.startswith("-") or line.startswith("+"))
46+
and "POT-Creation-Date:" not in line
47+
and "PO-Revision-Date:" not in line
48+
and po_file not in line
49+
)
50+
51+
52+
def revert_po_file(changeset, po_file, logger):
53+
"""
54+
Reverts a PO file to its original state in the given changeset.
55+
"""
56+
ref = changeset.split("...")[
57+
0
58+
] # Use the first part of the changeset as the reference
59+
logger.info(f"{po_file} has no meaningful changes, reverting to {ref}")
60+
cmd = ["git", "show", f"{ref}:{po_file}"]
61+
proc = subprocess.run(cmd, capture_output=True, text=True)
62+
proc.check_returncode()
63+
64+
with open(po_file, "w") as file:
65+
file.write(proc.stdout)
66+
67+
68+
def main(argv: typing.Optional[typing.List[str]] = None) -> int:
69+
logging.basicConfig(
70+
format="[%(levelname)s] %(message)s", level=logging.INFO
71+
)
72+
logger = logging.getLogger(__name__)
73+
74+
parser = argparse.ArgumentParser(
75+
description="Revert PO files with only timestamp changes."
76+
)
77+
parser.add_argument("--from-ref", help="Use changes since this commit.")
78+
parser.add_argument("--to-ref", help="Use changes until this commit.")
79+
parser.add_argument("files", nargs="*", help="Only check these files.")
80+
args = parser.parse_args(argv)
81+
82+
# Remove the first entry which is the script file name itself
83+
files_to_process = args.files[1:] if args.files else []
84+
85+
changeset = get_git_changeset(args.from_ref, args.to_ref)
86+
87+
for po_file in files_to_process:
88+
count = count_meaningful_changes(changeset, po_file)
89+
if count == 0:
90+
revert_po_file(changeset, po_file, logger)
91+
92+
return 0
93+
94+
95+
if __name__ == "__main__":
96+
sys.exit(main())

0 commit comments

Comments
 (0)