【发布时间】:2012-03-01 17:29:03
【问题描述】:
有没有办法使用钩子脚本在 Smartgit 中自动插入提交消息? (重击)。 如果用户提交了他的更改,我想预加载提交消息字段。
【问题讨论】:
有没有办法使用钩子脚本在 Smartgit 中自动插入提交消息? (重击)。 如果用户提交了他的更改,我想预加载提交消息字段。
【问题讨论】:
很遗憾,SmartGit 不支持 pre-commit git hook。
但是,从 SmartGit 18.2 开始,支持 commit message templates (SmartGit What's new)。遗憾的是,这些模板是静态的。
如果像我一样,您的目标是根据分支名称预加载提交消息,您可以使用一种解决方法,每次触发 post-checkout git hook 时动态生成静态提交消息模板。
这就是它的工作原理:
首先,安装一个 git post-checkout 钩子,该钩子根据分支名称编写提交消息模板。例如,如果您的功能分支名称是 ISSUE-123/feature/new-awesome-feature,并且您希望提交消息以问题键 ISSUE-123 为前缀,则可以使用以下脚本(我更喜欢 Python):
#!/usr/bin/env python3
import pygit2
GIT_COMMIT_TEMPLATE = ".git/.commit-template"
def main():
branch_name = pygit2.Repository('.').head.shorthand
issue_key = branch_name.split('/')[0]
with open(GIT_COMMIT_TEMPLATE, "w") as file:
file.write(f"{issue_key}: ")
if __name__ == "__main__":
main()
其次,配置 git commit 模板。使用上面示例中的文件名,我们得到:
git config commit.template .git/.commit-template
奖励提示:
git config --global core.hooksPath /path/to/my/centralized/hooks
并全局安装提交模板:
git config --global commit.template .git/.commit-template
post-checkout 脚本中,可以从 git 配置中提取 git 提交消息模板文件路径。例如:pygit2.Repository(".").config["commit.template"]
【讨论】:
您可能会感兴趣的钩子有 2 个: 准备提交消息和提交消息
prepare-commit-msg 可能更适合您的目的,因为它允许您在用户看到之前预先填写提交消息。不幸的是 Smartgit 不支持这个钩子。 (参见My post 和它所指的两个较早的帖子)
commit-msg 还允许您修改提交消息,但这样做是在用户发送消息之后。您的 .git/hooks 目录中的示例钩子脚本应该可以为您编写自己的代码提供一个良好的开端。
Git 钩子比模板更通用。模板更易于使用。如果您预加载的提交消息没有任何动态或需要 shell 脚本才能运行,那么模板可能是更合适的路线。要使用模板,您必须在git-config 中设置commit.template 选项。要在 Smartgit 中进行设置,请转到“工具”>“打开 git shell”,然后键入
git config commit.template tmplfile
其中 tmplfile 是包含您的提交消息模板的文件,其中包括您的 git 项目根目录的路径。
【讨论】:
【讨论】:
prepare-commit-msg 挂钩。这是一个无赖。