forked from kubernetes/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a script to check differences between translated and English vers…
…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
1 parent
846de91
commit be946f2
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |