Smartgit: Auto insert commit message

后端 未结 3 1475
深忆病人
深忆病人 2021-02-15 01:42

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:19

    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"]
    
    0 讨论(0)
  • 2021-02-15 02:24

    I don't see any SmartGit configuration for this feature.

    I would rather rely on a prepare-commit-msg hook as described in "How do I add project-specific information to the Git commit comment?", based on a commit.template Git configuration.
    See also "Including the current branch name in the commit template" for another example.

    0 讨论(0)
  • 2021-02-15 02:30

    There are 2 hooks which may be of interest to you: prepare-commit-msg and commit-msg

    prepare-commit-msg is probably the better suited to your purposes as it allows you to pre-fill the commit message before the user sees it. Unfortunately Smartgit does not support this hook. ( see My post and the two older posts to which it refers )

    commit-msg will also allow you to modify the commit message, but does so after the user has sent the messages. The example hook scripts in your .git/hooks directory should give you a good start on writing your own.

    Git hooks are more versatile than templates. Templates are simpler to use. If your preloaded commit message doesn't have anything dynamic or which would require a shell script to work out, a template may be the more appropriate route. To use a template you must set the commit.template option in git-config. To set this in Smartgit, go to "Tools">"Open git shell", then type

    git config commit.template tmplfile

    where tmplfile is the file which contains your commit message template including the path there from your git project's root.

    0 讨论(0)
提交回复
热议问题