0

GitPython 允许我处理 Git 工作副本。我想用它。但是我将如何使用 GitPython 获取唯一部分,即“缩写参考 ID”?

所以我对给我的--abbrev-commit选项感兴趣git log(例如在git log --abbrev-commit --pretty=oneline -n 1)。

我怎样才能在 GitPython 中得到它 - 或者我必须通过枚举 ref ID 并自己找出所需的长度来实现它?

4

1 回答 1

3

以下代码是有关如何使用git rev-parse --shortGitPython 功能的示例:

import git
r = git.Repo()
short = r.git.rev_parse(r.head, short=True)
print(short)
# u'f360ecd'

似乎使用 git-command 比自己以安全的方式实现它更可取。

一个天真的纯 python 实现可能看起来像这样:

r.head.commit.hexsha[:7]

但是,它不会在创建时检查获得的前缀是否真正唯一,这就是为什么应该首选基于 git-command 的方法。

于 2015-07-21T09:10:16.483 回答