Why use Git Hook

As teams grow and projects become more complex, there will be a set of specifications for Git development workflows, and there will generally be similar requirements for managing code and unifying styles.

At this time, only rely on the members of the conscious operation, the omission of operation is inevitable.

Obviously it would be more reliable to go through the machine for each inspection.

Today’s test students proposed to rely on commit information, after Jenkins packaging, to automatically change the corresponding test platform bug list. The prefix [Fix] or [ref-bug] is required.

It is now only required to standardize the commit format of git Commit. Format, such as:

[Function] Complete the chat module

[Bug] Fixed sliding out of bounds crash

[Modify] Modify personal copy

Just before writing other scripts, I heard from my colleague about the use of Git Hook, I think it is suitable for this scenario.

After learning to use it, you will be able to deal with more complex Git development workflow requirements in the future.

Git Hook to introduce

Like Jenkins’ CI systems, Git can trigger scripts when certain important actions occur.

Git Hook capabilities can be integrated into Git development workflows based on each team’s own code quality requirements.

Git Hook classification

Git Hook is also divided into two sides: client side and server side.

The client-side Git hooks work on our local machine, and the server-side Git hooks work on the remote server repository that we commit to.

Git Hook:

  • Pre-commit: triggered when the git commit command is executed. It is used to check the code style
  • Prepare-commit-msg: triggered before the commit Message editor calls and after the default commit message is created, it is used to generate the default standardized commit description
  • Commit-msg: triggered after the developer has written and confirmed the commit message. It is used to verify that the commit instructions are standard
  • Post-commit: Triggered after the git commit is complete. It is used for email notification and reminder
  • Applypatch -msg: triggered when the git am command is executed. It is used to check whether the submitted information extracted from the command meets a specific format
  • Pre-applypatch: Git AM is triggered before commit after patch has been extracted and applied to the current branch. It is often used to execute test cases or check buffer code
  • Post-applypatch: Triggered after git am has been committed. This hook is used for notification or patch email reply.
  • Pre-rebase: triggered when the git rebase command is executed
  • Post-rewrite: triggered when executing commands that replace commit, such as git rebase or git commit –amend
  • Post-checkout: Triggered when the git checkout command is executed successfully, it can be used to generate specific documents, handle large binaries, and so on
  • Post-merge: Triggered after a merge operation is successfully completed
  • Pre-push: triggered when the git push command is executed. It can be used to execute test cases
  • Pre-auto-gc: trigger before garbage collection

Git Hook:

  • Pre-receive: Triggered when the server receives a push request, it detects push content
  • Update: The update script is very similar to the pre-receive script, except that it runs once for each branch that is ready to be updated. Pre-receive only runs once if a pusher pushes content to multiple branches at the same time, whereas Update runs once for each branch that is pushed.
  • Post-receive: The post-receive hook is run at the end of the process and can be used to update other system services or notify users. Its uses include sending a message to a mailing list to notify the continous Integration server, Or update ticket-tracking systems — or even analyze submitted information to determine whether a ticket should be turned on, changed, or closed.

Use Git Hook

Git hooks are stored in the.git/hooks folder.

- YourGitRepo
  |- .git
     |- hooks
        |- hooks--commit-msg.sample
        |- hooks--post-update.sample
        ...

Copy the code

The complete.sample files are available in Git’s open source repository: Git Hooks sample.

For those of you like me, the company’s GitLab repository does not automatically generate hooks directories, so don’t worry, just create an hooks folder in the.git directory as shown above.

At this point, you can write shell scripts to manipulate Git whenever you want.

Note if it is a sample file, remove the. Sample suffix and change it to the file name mentioned in Git Hook classification.

For example, if I want to verify the commit information, the Git Hook script name should be commit-msg.

Hooks –commit — msg.sample:

#! /bin/sh
#
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message.  The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
#commit. The hook is allowed to edit the commit message file.
#
# To enable this hook, rename this file to "commit-msg".

# Uncomment the below to add a Signed-off-by line to the message.
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
# hook is more suited to it.
#
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "The $1" || echo "$SOB" >> "The $1"

# This example catches duplicate Signed-off-by lines.

test "" = "$(grep '^Signed-off-by: ' "$1" |
	 sort | uniq -c | sed -e '/^[ 	]*1[ 	]/d')" || {
	echo >&2 Duplicate Signed-off-by lines.
	exit 1
}

Copy the code

Git /COMMIT_EDITMSG: commit MSG ($1)

Using commands:

msg=$(cat $1)
Copy the code

You can take the commit information and, with a few modifications, verify the team’s specification of commit information as I said.

Git Hook does not work

If the script does not work, the script execution permission may not be enabled, so the script cannot be executed. That’s what happened to me.

Go to the. Git /hooks directory and run the chmod 777 command.

chmod 777 commit-msg
Copy the code

Since the needs of each team are different, I won’t go into detail here.

I’ve uploaded the commit-msg code to GitHub for those interested to see how it works.

This is just a very simple practice of Git Hook, it can be used in more scenarios, more specific situations need to be treated in detail.

reference

Custom Git – Git hooks

Git Hook practice experience