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表示只显示最后一次提交

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Git基础 获取Git仓库的两种方式 将尚未进行版本控制的本地目录转换为Git仓库 从其他服务器==克隆==一个已...
    squarejohn_5dc8阅读 2,669评论 0 1
  • git branch 查看本地所有分支 git status 查看当前状态 git commit 提交 git b...
    猿万阅读 10,528评论 1 45
  • 一、 Git 常用命令速查 git branch 查看本地所有分支 git status 查看当前状态 git c...
    LOVE_晴天阅读 6,818评论 0 10
  • git常用命令 GIT常用命令备忘:http://stormzhang.com/git/2014/01/27/gi...
    新篇章阅读 12,745评论 1 26
  • 1,查看所有远程分支:%git branch -r 2, 拉取远程分支并创建本地分支git checkout -...
    will666阅读 6,268评论 0 18