python - 如何编写 python 预推送来检查提交消息?

标签 python git githooks

我知道我可以使用 commit-msg 钩子(Hook)来检查消息(我做到了)但我希望在使用不同的标准推送之前再次检查消息。

.git/hooks下给出的例子是用shell脚本写的,但是我想写一个python脚本我想到的字符串操作比较复杂。

我知道我可以将第一行更改为 #!/usr/bin/env python。我的问题是,我不知道如何获取最新提交的消息字符串。我尝试了 git rev-list(在该示例的第 44 行),但它没有给我消息字符串,它需要提交哈希 ID。请注意,消息字符串可能是多行的(因为第一行被限制为最多 50 个字符)。

EIDT:其他题都是问如何用Python写pre-push脚本,但是没有涉及到消息串的检查。

最佳答案

pre-push hook传递了本地/远程引用和提交 ID (sha1) 的列表,因此您必须逐行读取它们,拆分并获取本地提交 ID。 See如何在 shell 脚本中完成。

有了提交 ID,您可以使用命令提取完整的提交消息

git show --format='%B' -s $SHA1

在 Python 中是这样的:

for line in sys.stdin:
    local_ref, local_sha1, remote_ref, remote_sha1 = line.strip().split()
    message = subprocess.check_output(
        ['git', 'show', '--format=%B', '-s', local_sha1])
    if not check(message):
        sys.exit(1)

关于python - 如何编写 python 预推送来检查提交消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50070157/

相关文章:

git - 使用 git post-merge 修复权限

git - 在 Azure DevOps 中 pull 请求后 Hook oder 任务以修改文件

git - 获取 Git 存储库的最后一次提交

git - 如果我使用 Atlassian Stash,是否仍可以在不使用 Stash 的情况下管理 git 存储库?

git - Git 中的 commit-ish 和 tree-ish 是什么?

python - 如何避免 evoque 中的 html 转义

python - virtualenvwrapper.user_scripts 找不到 get_env_details

Git:我可以将我的工作目录提交到一个新分支而不将它提交到当前分支吗?

python - Cookiecutter 创建的目录给我运行开发服务器和 python shell 的问题

Python: Can dumpdata cannot loaddata back. Unicode解码错误