|
24 | 24 | """
|
25 | 25 |
|
26 | 26 | import os
|
| 27 | +import re |
27 | 28 | import time
|
28 | 29 | import logging
|
29 | 30 | from argparse import ArgumentParser, ArgumentTypeError, BooleanOptionalAction
|
@@ -69,6 +70,38 @@ def get_origin_from_trans(origin_path, t_from_head):
|
69 | 70 | return o_from_t
|
70 | 71 |
|
71 | 72 |
|
| 73 | +def get_origin_from_trans_smartly(origin_path, t_from_head): |
| 74 | + """Get the latest origin commit from the formatted translation commit: |
| 75 | + (1) update to commit HASH (TITLE) |
| 76 | + (2) Update the translation through commit HASH (TITLE) |
| 77 | + """ |
| 78 | + # catch flag for 12-bit commit hash |
| 79 | + HASH = r'([0-9a-f]{12})' |
| 80 | + # pattern 1: contains "update to commit HASH" |
| 81 | + pat_update_to = re.compile(rf'update to commit {HASH}') |
| 82 | + # pattern 2: contains "Update the translation through commit HASH" |
| 83 | + pat_update_translation = re.compile(rf'Update the translation through commit {HASH}') |
| 84 | + |
| 85 | + origin_commit_hash = None |
| 86 | + for line in t_from_head["message"]: |
| 87 | + # check if the line matches the first pattern |
| 88 | + match = pat_update_to.search(line) |
| 89 | + if match: |
| 90 | + origin_commit_hash = match.group(1) |
| 91 | + break |
| 92 | + # check if the line matches the second pattern |
| 93 | + match = pat_update_translation.search(line) |
| 94 | + if match: |
| 95 | + origin_commit_hash = match.group(1) |
| 96 | + break |
| 97 | + if origin_commit_hash is None: |
| 98 | + return None |
| 99 | + o_from_t = get_latest_commit_from(origin_path, origin_commit_hash) |
| 100 | + if o_from_t is not None: |
| 101 | + logging.debug("tracked origin commit id: %s", o_from_t["hash"]) |
| 102 | + return o_from_t |
| 103 | + |
| 104 | + |
72 | 105 | def get_commits_count_between(opath, commit1, commit2):
|
73 | 106 | """Get the commits count between two commits for the specified file"""
|
74 | 107 | command = f"git log --pretty=format:%H {commit1}...{commit2} -- {opath}"
|
@@ -108,7 +141,10 @@ def check_per_file(file_path):
|
108 | 141 | logging.error("Cannot find the latest commit for %s", file_path)
|
109 | 142 | return
|
110 | 143 |
|
111 |
| - o_from_t = get_origin_from_trans(opath, t_from_head) |
| 144 | + o_from_t = get_origin_from_trans_smartly(opath, t_from_head) |
| 145 | + # notice, o_from_t from get_*_smartly() is always more accurate than from get_*() |
| 146 | + if o_from_t is None: |
| 147 | + o_from_t = get_origin_from_trans(opath, t_from_head) |
112 | 148 |
|
113 | 149 | if o_from_t is None:
|
114 | 150 | logging.error("Error: Cannot find the latest origin commit for %s", file_path)
|
|
0 commit comments