Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git pre-status or post-status hook

Tags:

git

bash

hook

I would like to run a linter on git status, however there seems to be no pre-status nor post-status hook. How could one add a hook to git? The fine docs are suspiciously silent on the matter!

I'm currently wrapping my linter and git status in a Bash script, but I would prefer a solution which supports my muscle-memory-macro git status. I'm running CentOS 7.3 with KDE 4 if it matters.

like image 770
dotancohen Avatar asked Jan 25 '17 09:01

dotancohen


People also ask

What is pre-commit hook in git?

The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.

What is post commit hook in git?

The post-commit hook is called immediately after the commit-msg hook. It can't change the outcome of the git commit operation, so it's used primarily for notification purposes. The script takes no parameters and its exit status does not affect the commit in any way.

Do git hooks get pushed?

No. Hooks are per-repository and are never pushed.

How do you do a pre-commit hook?

Open a terminal window by using option + T in GitKraken Client. Once the terminal windows is open, change directory to . git/hooks . Then use the command chmod +x pre-commit to make the pre-commit file executable.


1 Answers

Git hooks are for operations that (are going to) modify the repository or the working tree. Since git status is a read-only operation there is no hook for it.

I'm currently wrapping my linter and git status in a Bash script, but I would prefer a solution which supports my muscle-memory-macro git status.

You can wrap your git command into the following function that will not require to adjust your muscle memory:

git()
{
    if [[ $# -ge 1 && "$1" == "status" ]]
    then
        echo Your git-status pre-hook should be here
    fi

    command git "$@"
}
like image 93
Leon Avatar answered Oct 26 '22 23:10

Leon