-
Notifications
You must be signed in to change notification settings - Fork 14.7k
/
Copy pathupstream_changes.py
executable file
·78 lines (63 loc) · 1.95 KB
/
upstream_changes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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()