有没有办法使用钩子脚本在Smartgit中自动插入提交消息? (击)。 如果用户提交了他的更改,我想预先加载提交消息字段。
答案 0 :(得分:7)
我没有看到此功能的SmartGit configuration。
我希望依据“prepare-commit-msg
hook”中所述的How do I add project-specific information to the Git commit comment?,基于commit.template
Git configuration。
有关其他示例,另请参阅“Including the current branch name in the commit template”。
答案 1 :(得分:2)
您可能感兴趣的是2个钩子: prepare-commit-msg和commit-msg
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项目根目录中的路径。
答案 2 :(得分:0)
很不幸,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 commit消息模板文件路径。例如:pygit2.Repository(".").config["commit.template"]