Skip to content

Commit

Permalink
Add a script to check differences between translated and English vers…
Browse files Browse the repository at this point in the history
…ion (kubernetes#14731)

This script will query all the changes that happen to a reference document (in English) since the last time a given translation was merged (For instance in French).
This could help all the translation team to keep up with the changes that happened inside the reference document.
  • Loading branch information
remyleone authored and k8s-ci-robot committed Jun 12, 2019
1 parent 846de91 commit be946f2
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions scripts/upstream_changes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python

import re
from subprocess import check_output

import click


def last_commit(path, git):
"""
Find the hash of the last commit that touched a file.
"""
cmd = [git, "log", "-n", "1", "--pretty=format:%H", "--", path]
try:
return check_output(cmd)
except Exception as exc:
raise exc


def diff(reference_commit_hash, translation_commit_hash, reference_path, git):
"""
Returns the diff between two hashes on a specific file
"""
cmd = [git, "diff",
"%s...%s" % (translation_commit_hash, reference_commit_hash),
"--",
reference_path]
try:
return check_output(cmd)
except Exception as exc:
raise exc


def find_full_path(path, git):
cmd = [git, "ls-tree",
"--name-only", "--full-name", "HEAD",
path]
try:
return check_output(cmd).strip()
except Exception as exc:
raise exc


def find_reference(path, git):
abs_path = find_full_path(path, git=git)
return re.sub('content/(\w{2})/', 'content/en/', abs_path)


@click.command()
@click.argument("path")
@click.option("--reference", "reference",
help="Specify the reference version of the file. Default to the English one.",
default=None)
@click.option("--git-path",
"git",
help="Specify git path",
default="git")
def main(path, reference, git):
"""
Find what changes occurred between two versions
ex:
./upstream_changes.py content/fr/_index.html
"""
if reference is None:
reference = find_reference(path, git=git)
reference_commit_hash = last_commit(path=reference, git=git)
translation_commit_hash = last_commit(path=path, git=git)

print(diff(
reference_commit_hash=reference_commit_hash,
translation_commit_hash=translation_commit_hash,
reference_path=reference,
git=git
))

if __name__ == '__main__':
main()

0 comments on commit be946f2

Please sign in to comment.