Can Git hook scripts be managed along with the repository?

前端 未结 11 1056
独厮守ぢ
独厮守ぢ 2020-11-22 14:00

We\'d like to make a few basic hook scripts that we can all share -- for things like pre-formatting commit messages. Git has hook scripts for that that are normally stored

相关标签:
11条回答
  • 2020-11-22 14:14

    For gradle users

    I found these scripts very useful for gradle projects.

    build.gradle

    apply from: rootProject.file('gradle/install-git-hooks.gradle')

    gradle/install-git-hooks.gradle

    tasks.create(name: 'gitExecutableHooks') {
        doLast {
            Runtime.getRuntime().exec("chmod -R +x .git/hooks/");
        }
    }
    task installGitHooks(type: Copy) {
        from new File(rootProject.rootDir, 'pre-commit')
        into { new File(rootProject.rootDir, '.git/hooks') }
    }
    gitExecutableHooks.dependsOn installGitHooks
    clean.dependsOn gitExecutableHooks
    

    pre-commit

    .... your pre commit scripts goes here
    
    0 讨论(0)
  • 2020-11-22 14:15

    In Git 2.9, the configuration option core.hooksPath specifies a custom hooks directory.

    Move your hooks to a hooks tracked directory in your repository. Then, configure each instance of the repository to use the tracked hooks instead of $GIT_DIR/hooks:

    git config core.hooksPath hooks
    

    In general, the path may be absolute, or relative to the directory where the hooks are run (usually the working tree root; see DESCRIPTION section of man githooks).

    0 讨论(0)
  • 2020-11-22 14:15

    Most of the modern programming languages, or rather their build tools, support plugins to manage git hooks. That means all you need to do is configure your package.json, pom.xml, etc., and anyone in your team will have no option but to comply unless they change the build file. The plugin will add content to .git directory for you.

    Examples:

    https://github.com/rudikershaw/git-build-hook

    https://github.com/olukyrich/githook-maven-plugin

    https://www.npmjs.com/package/git-hooks

    0 讨论(0)
  • 2020-11-22 14:17

    You can make your hooks folder another git repository and link it as a submodule... I guess worth it only if you have a lot of members and hooks changed regularly.

    0 讨论(0)
  • 2020-11-22 14:21

    If your project is a JavaScript project and you use npm as package manager you can use shared-git-hooks to enforce githooks on npm install.

    0 讨论(0)
  • 2020-11-22 14:22

    For Nodejs users a simple solution is to update package.json with

    {
      "name": "name",
      "version": "0.0.1",
      ......
      "scripts": {
        "preinstall": "git config core.hooksPath hooks", 
    

    The preinstall will run before

    npm install

    and redirects git to look for hooks inside the .\hooks (or whatever name you choose) directory. This directory should mimic .\.git\hooks in terms of file name (minus the .sample) and structure.

    Imagine Maven and other build tools will have an equivalent to preinstall.

    It should also work across all platforms.

    If you need any more info see https://www.viget.com/articles/two-ways-to-share-git-hooks-with-your-team/

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