如何使用 GitPython 获取参考 ID 的唯一部分?
How to get the unique part of a ref ID with GitPython?
GitPython 允许我处理 Git 个工作副本。我想用它。但是我如何使用 GitPython 获取唯一部分,即 "abbreviated ref ID"?
所以我对 git log
的 --abbrev-commit
选项给我的内容感兴趣(例如 git log --abbrev-commit --pretty=oneline -n 1
)。
我如何在 GitPython 中获得它 - 或者我是否必须通过枚举 ref ID 并自己计算出所需的长度来实现它?
以下代码是如何使用 GitPython 中的 git rev-parse --short
功能的示例:
import git
r = git.Repo()
short = r.git.rev_parse(r.head, short=True)
print(short)
# u'f360ecd'
看起来使用 git 命令比自己以 安全 方式实现它更可取。
天真的纯 python 实现可能看起来像这样:
r.head.commit.hexsha[:7]
但是,它不会在创建时检查获取的前缀是否真正唯一,这就是为什么应该首选 git-基于命令的方法。
GitPython 允许我处理 Git 个工作副本。我想用它。但是我如何使用 GitPython 获取唯一部分,即 "abbreviated ref ID"?
所以我对 git log
的 --abbrev-commit
选项给我的内容感兴趣(例如 git log --abbrev-commit --pretty=oneline -n 1
)。
我如何在 GitPython 中获得它 - 或者我是否必须通过枚举 ref ID 并自己计算出所需的长度来实现它?
以下代码是如何使用 GitPython 中的 git rev-parse --short
功能的示例:
import git
r = git.Repo()
short = r.git.rev_parse(r.head, short=True)
print(short)
# u'f360ecd'
看起来使用 git 命令比自己以 安全 方式实现它更可取。
天真的纯 python 实现可能看起来像这样:
r.head.commit.hexsha[:7]
但是,它不会在创建时检查获取的前缀是否真正唯一,这就是为什么应该首选 git-基于命令的方法。