📜  获取 git sha - Python 代码示例

📅  最后修改于: 2022-03-11 14:47:06.731000             🧑  作者: Mango

代码示例1
def get_git_sha():
    locations_to_try = ['git', '/bin/git', '/usr/bin/git', '/usr/local/bin/git']
    git_binary, sha, timestamp = None, None, None
    for location in locations_to_try:
        sha = os.popen(f'{location} rev-parse HEAD').read().strip()
        if (len(sha) > 10) and ' ' not in sha:
            git_binary = str(location)
            break
    if not sha:
        return None
    timestamp = os.popen(f"{git_binary} show --no-patch --no-notes --pretty='%at' {sha}").read().strip()
    timestamp = str(datetime.fromtimestamp(int(timestamp)))
    return git_binary, sha, timestamp