python - 使用 gitpython 获取 commit 的简短 sha

标签 python git gitpython

长SHA可以像下面这样得到:

repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha

或者,在 git 3.1.7 中:

sha = repo.head.commit.hexsha

短的怎么样? (短 SHA 由 repo 的规模决定,所以它不应该像 sha[:7])

最佳答案

据我所知,gitpython Commit 对象不直接支持short sha。但是,您仍然可以使用 gitpython 的 support for calling git directly检索它(从 gi​​t 3.1.7 开始):

repo = git.Repo(search_parent_directories=True)
sha = repo.head.commit.hexsha
short_sha = repo.git.rev_parse(sha, short=4)

这相当于运行

git rev-parse --short=4 ...

在命令行上,即 the usual way of getting the short hash .这将返回长度 >= 4 的最短可能的明确散列(您可以传入较小的数字,但由于 git 的内部最小值是 4,因此它会产生相同的效果)。

关于python - 使用 gitpython 获取 commit 的简短 sha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31956506/

相关文章:

python - pandas df 中的多重逻辑比较

python - Python网页抓取,如果出现错误,请跳过网址

git - 各个 git 存储库中的 Terraform Kubernetes 服务定义?

git - 如何仅使用分支名称而不是单个提交来鸟瞰 git 树?

proxy - 使用 GitPython 克隆代理后面的存储库

python - 为什么我收到 "ImportError: No module named pyramid.config"

python - R/Python 网站抓取

Git 从 fork 的存储库中 pull

python - 如何通过 git-python 添加/提交和推送存储库?

python - 在 GitPython 中 checkout 或列出远程分支