长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检索它(从 git 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/