Skip to content

Commit

Permalink
Add a script that reverts changes in po files when only the time stam…
Browse files Browse the repository at this point in the history
…p has changed

It helps to create meaningful commits, instead of up to 4100 changed files.
  • Loading branch information
daschuer committed Dec 29, 2024
1 parent 76bc7b0 commit 8a6c2dc
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ repos:
types: [rst]
entry: python tools/fixup_gh_wiki_anchors.py
language: python
- id: ignore-pot-creation-date
name: Ignore POT-Creation-Date
entry: python tools/ignore_pot_creation_date.py
language: python
files: ^.*\.po$
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.1.0
hooks:
Expand Down
96 changes: 96 additions & 0 deletions tools/ignore_pot_creation_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import argparse
import logging
import os
import subprocess
import sys
import typing

"""
This script reverts changes in PO files when only the timestamp has changed.
It helps to create meaningful commits by ignoring unnecessary changes.
"""


def get_git_changeset(from_ref, to_ref) -> str:
"""
Constructs the changeset string for `git diff` based on from_ref and
to_ref.
"""
from_ref = (
from_ref
or os.getenv("PRE_COMMIT_FROM_REF")
or os.getenv("PRE_COMMIT_SOURCE")
or "HEAD"
)
to_ref = (
to_ref
or os.getenv("PRE_COMMIT_TO_REF")
or os.getenv("PRE_COMMIT_ORIGIN")
)
return f"{from_ref}...{to_ref}" if to_ref else from_ref


def count_meaningful_changes(changeset, po_file) -> int:
"""
Counts meaningful changes in the diff for a given PO file.
"""
cmd = ["git", "diff", "--patch", "--unified=0", changeset, "--", po_file]
proc = subprocess.run(cmd, capture_output=True, text=True)
proc.check_returncode()

diff_lines = proc.stdout.splitlines()
return sum(
1
for line in diff_lines
if (line.startswith("-") or line.startswith("+"))
and "POT-Creation-Date:" not in line
and "PO-Revision-Date:" not in line
and po_file not in line
)


def revert_po_file(changeset, po_file, logger):
"""
Reverts a PO file to its original state in the given changeset.
"""
ref = changeset.split("...")[
0
] # Use the first part of the changeset as the reference
logger.info(f"{po_file} has no meaningful changes, reverting to {ref}")
cmd = ["git", "show", f"{ref}:{po_file}"]
proc = subprocess.run(cmd, capture_output=True, text=True)
proc.check_returncode()

with open(po_file, "w") as file:
file.write(proc.stdout)


def main(argv: typing.Optional[typing.List[str]] = None) -> int:
logging.basicConfig(
format="[%(levelname)s] %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)

parser = argparse.ArgumentParser(
description="Revert PO files with only timestamp changes."
)
parser.add_argument("--from-ref", help="Use changes since this commit.")
parser.add_argument("--to-ref", help="Use changes until this commit.")
parser.add_argument("files", nargs="*", help="Only check these files.")
args = parser.parse_args(argv)

# Remove the first entry which is the script file name itself
files_to_process = args.files[1:] if args.files else []

changeset = get_git_changeset(args.from_ref, args.to_ref)

for po_file in files_to_process:
count = count_meaningful_changes(changeset, po_file)
if count == 0:
revert_po_file(changeset, po_file, logger)

return 0


if __name__ == "__main__":
sys.exit(main())

0 comments on commit 8a6c2dc

Please sign in to comment.