Smartgit: Auto insert commit message

后端 未结 3 1405
孤独总比滥情好
孤独总比滥情好 2021-02-15 01:38

Is there a way to auto insert the commit message in Smartgit with a hook script? (Bash). If a user commit\'s his change, I want to preload the commit message field.

3条回答
  •  孤独总比滥情好
    2021-02-15 02:28

    Unfortunately, SmartGit does not support pre-commit git hook. However, since SmartGit 18.2, commit message templates are supported (SmartGit What's new). Sadly, these templates are static.

    If, like me, your goal is to preload a commit message based on the branch name, you can use a workaround in which a static commit message template is generated dynamically every time the post-checkout git hook is triggered.

    This is how it works:

    First, install a git post-checkout hook that writes a commit message template based on the branch name. For example, if your feature branches names are ISSUE-123/feature/new-awesome-feature, and you want the commit message to be prefixed with the issue key ISSUE-123, then the following script could be used (I prefer 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()
    

    Second, configure git commit template. Using the filename from the example above, we get:

    git config commit.template .git/.commit-template
    

    Bonus tips:

    1. It’s possible to use the same configuration across all repositories, by using a global git hooks path (as seen here):
    git config --global core.hooksPath /path/to/my/centralized/hooks
    

    and installing the commit template globally:

    git config --global commit.template .git/.commit-template
    
    1. In the post-checkout script it’s possible to extract the the git commit message template file path from the git configuration. For example:
    pygit2.Repository(".").config["commit.template"]
    

提交回复
热议问题