Files
MaaAssistantArknights/tools/AutoLocalization/src/auto_localization/git.py
quyansiyuanwang 490cfbf033 perf & refactor: 重构 tools/AutoLocalization (#6995)
其中包含部分更优化和跟规范的算法或结构

---------

Co-authored-by: David <56174894+Constrat@users.noreply.github.com>
2023-10-21 16:17:34 +02:00

30 lines
912 B
Python

import os.path
import subprocess
def get_latest_file_content(file_path="./cli.py", encoding="utf-8", tag_name=""):
dirname = os.path.dirname(file_path)
basename = os.path.basename(file_path)
# 获取最近一次修改该文件的commit的ID
heading = "HEAD"
if tag_name:
result = subprocess.run(
["git", "rev-list", "-n", "1", "tag-autolocalization", "--", basename],
cwd=dirname,
capture_output=True,
text=True,
)
commit_id = result.stdout.replace('"', "").replace("'", "").strip()
heading = commit_id if commit_id else heading
result = subprocess.run(
["git", "show", f"{heading}:./{basename}"],
cwd=dirname,
capture_output=True,
text=True,
encoding=encoding,
)
return result.stdout
if __name__ == "__main__":
print(get_latest_file_content("cli.py"))