Python获取git文件的版本号

import os

import subprocess

cmd = "cd {} & git log --pretty=format:\"%H\" -1 {}"

def runCmd(command):
    ret = subprocess.run(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,timeout=1)
    return ret

def getGitFileVersion(file):
    # print("getGitFileVersion " + file)
    strs = os.path.split(file)
    dir,fileName = strs[0],strs[1]
    content = str(runCmd(cmd.format(dir,fileName)).stdout,"utf-8")
    # print(content)
    return content

利用git log 命令,--pretty=format:\"%H\"表示输出版本hash,-1表示只显示最后一次提交

你可能感兴趣的:(Python获取git文件的版本号)