美文网首页
Python获取git文件的版本号

Python获取git文件的版本号

作者: 最怕认真 | 来源:发表于2022-03-10 13:25 被阅读0次
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文件的版本号

      本文链接:https://www.haomeiwen.com/subject/yamfdrtx.html