简体   繁体   中英

Is it possible to commit the pre-commit hooks?

We set up a pre-commit hook for our Python project. The pre-commit sits in the .git folder which is untracked (for good reason).

But it's quite common for someone new to clone a repo and forget to get up the commit hook. Is it possible to automate this? Eg by having the commit hook tracked by git? Or via some other mechanism eg when git clone is called the hook is automatically set up.

For example, adding the ..git/hooks/pre-commmit to .gitignore doesn't do the trick.

No it's not possible.

(Funny that this answers your question, I'd suggest to rename it as "how to commit the pre-commit hook")

I'm not sure how to do this in python project that don't have build-system. You could add some lines to the readme file.

What we are doing in dotnet and android project: we add to our project buid a stage that would set-up a hook. And it's highly likely that the developer would build the project at least once before committing.

We have directory /git_hooks and script that copies files from it:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Message Text="Install Git Hooks" Importance="high" />
    <Copy SourceFiles="$(SolutionDir)/git_hooks/pre-commit" 
          DestinationFolder="$(SolutionDir)/.git/hooks" 
          ContinueOnError="true" 
          SkipUnchangedFiles="true" />
</Target>

or

buildscript {
   //.....
    copy {
        from "./git_hooks"
        into "./.git/hooks"
        fileMode 0777
    }
}

on Android.

You can't do this automatically because hooks can run arbitrary code and any mechanism to automatically set up hooks would be a security vulnerability, since it would let the owners of the repository execute arbitrary code on a developer machine.

However, you may wish to check them into a place in the repository and then set up a standard script (such as the kind GitHub uses ) which installs hooks and prepares the repository for use.

I would, however, encourage you to read the Git FAQ entry about pre-commit hooks . It points out that they are there to help developers and not as an effective control, since they can be easily bypassed without notice. In addition, many developers who use a workflow with frequent squashing and rebasing (of which I am one) find pre-commit hooks to be an irritating impediment to their standard workflow since many temporary commits are created and destroyed. You are therefore better off using a CI system to verify things like tests and code style and make hooks an opt-in for developers if they'd like to use them.

It's possible but not recommended.

When a repository is initialized, it copies hooks from git template . We can create a post-checkout in the template. Here's a sample:

#!/bin/bash

gitworktree=$(git rev-parse --show-toplevel)
if [[ -f ${gitworktree}/pre-commit ]];then
    echo deploy pre-commit
    cp -v ${gitworktree}/pre-commit ${gitworktree}/.git/hooks/pre-commit
    chmod a+x ${gitworktree}/.git/hooks/pre-commit
fi

After git clone without -n , the post-checkout is copied to the new repository's .git/hooks automatically. And then the code is checked out and it invokes post-checkout . If pre-commit exists in the new repository, it's copied to .git/hooks . This is just a draft plan. There might be some issues in a real case. For example, how can we deal with the pre-commit which already exists in the template? Is it necessary to check if the pre-commit is a committed one? What if there are vicious codes in pre-commit ?

As other answers say, it's not a good idea. Don't do it in production envirionment.

Have a look at https://pre-commit.com/

You can create a separate repository with your hooks and use it in your projects. Usage is configured via YAML-files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM