在Python脚本中获取当前的git散列 [英] Get the current git hash in a Python script

查看:215
本文介绍了在Python脚本中获取当前的git散列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Python脚本的输出中包含当前的git散列(作为生成该输出的代码的版本号)。



如何在我的Python脚本中访问当前的git散列? git describe 命令是创建代码的人性化版本号的好方法。从文档中的例子:


使用git.git当前树,我得到:

  [torvalds @ g5 git] $ git describe parent 
v1.0.4-14-g2414721

即我的父分支的当前头部基于v1.0.4,但由于其上有几个提交,所以describe已经为提交本身添加了额外提交(14)的数量和缩写对象名称(2414721)。


在Python中,您可以执行以下操作:



$ p $ import subprocess
label = subprocess.check_output([git,describe])。strip()


I would like to include the current git hash in the output of a Python script (as a the version number of the code that generated that output).

How can I access the current git hash in my Python script?

解决方案

The git describe command is a good way of creating a human-presentable "version number" of the code. From the examples in the documentation:

With something like git.git current tree, I get:

[torvalds@g5 git]$ git describe parent
v1.0.4-14-g2414721

i.e. the current head of my "parent" branch is based on v1.0.4, but since it has a few commits on top of that, describe has added the number of additional commits ("14") and an abbreviated object name for the commit itself ("2414721") at the end.

From within Python, you can do something like the following:

import subprocess
label = subprocess.check_output(["git", "describe"]).strip()

这篇关于在Python脚本中获取当前的git散列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆