Vic*_*tor 139 python git git-hash
我想在Python脚本的输出中包含当前的git哈希(作为生成该输出的代码的版本号).
如何在Python脚本中访问当前的git哈希?
fif*_*nce 143
无需git亲自从命令中获取数据.GitPython是一个非常好的方法来做这个和许多其他的git东西.它甚至为Windows提供"尽力而为"的支持.
之后pip install gitpython,你可以做
import git
repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha
Run Code Online (Sandbox Code Playgroud)
Yuj*_*ita 91
import subprocess
def get_git_revision_hash():
return subprocess.check_output(['git', 'rev-parse', 'HEAD'])
def get_git_revision_short_hash():
return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'])
Run Code Online (Sandbox Code Playgroud)
Gre*_*ill 84
该git describe命令是创建代码的人类可呈现"版本号"的好方法.从文档中的示例:
有了类似git.git当前树的东西,我得到:
Run Code Online (Sandbox Code Playgroud)[torvalds@g5 git]$ git describe parent v1.0.4-14-g2414721即我的"父"分支的当前头部基于v1.0.4,但由于它上面有一些提交,因此describe添加了额外提交的数量("14")和提交的缩写对象名称本身("2414721")在最后.
在Python中,您可以执行以下操作:
import subprocess
label = subprocess.check_output(["git", "describe"]).strip()
Run Code Online (Sandbox Code Playgroud)
And*_*dyP 12
这是Greg 答案的更完整版本:
import subprocess
print(subprocess.check_output(["git", "describe", "--always"]).strip().decode())
Run Code Online (Sandbox Code Playgroud)
或者,如果脚本是从 repo 外部调用的:
import subprocess, os
print(subprocess.check_output(["git", "describe", "--always"], cwd=os.path.dirname(os.path.abspath(__file__))).strip().decode())
Run Code Online (Sandbox Code Playgroud)
或者,如果脚本是从 repo 外部调用的,并且您喜欢pathlib:
import subprocess
from pathlib import Path
print(subprocess.check_output(["git", "describe", "--always"], cwd=Path(__file__).resolve().parent).strip().decode())
Run Code Online (Sandbox Code Playgroud)
kag*_*ick 10
如果子进程不可移植并且您不想安装包来做这么简单的事情,您也可以这样做。
import pathlib
def get_git_revision(base_path):
git_dir = pathlib.Path(base_path) / '.git'
with (git_dir / 'HEAD').open('r') as head:
ref = head.readline().split(' ')[-1].strip()
with (git_dir / ref).open('r') as git_hash:
return git_hash.readline().strip()
Run Code Online (Sandbox Code Playgroud)
我只在我的 repos 上测试过这个,但它似乎工作得非常一致。
numpy有一个好看的多平台程序在其setup.py:
import os
import subprocess
# Return the git revision as a string
def git_version():
def _minimal_ext_cmd(cmd):
# construct minimal environment
env = {}
for k in ['SYSTEMROOT', 'PATH']:
v = os.environ.get(k)
if v is not None:
env[k] = v
# LANGUAGE is used on win32
env['LANGUAGE'] = 'C'
env['LANG'] = 'C'
env['LC_ALL'] = 'C'
out = subprocess.Popen(cmd, stdout = subprocess.PIPE, env=env).communicate()[0]
return out
try:
out = _minimal_ext_cmd(['git', 'rev-parse', 'HEAD'])
GIT_REVISION = out.strip().decode('ascii')
except OSError:
GIT_REVISION = "Unknown"
return GIT_REVISION
Run Code Online (Sandbox Code Playgroud)
这是Yuji 'Tomita' Tomita答案的改进。
import subprocess
def get_git_revision_hash():
full_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD'])
full_hash = str(full_hash, "utf-8").strip()
return full_hash
def get_git_revision_short_hash():
short_hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'])
short_hash = str(short_hash, "utf-8").strip()
return short_hash
print(get_git_revision_hash())
print(get_git_revision_short_hash())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54656 次 |
| 最近记录: |