VPS terminology
May 24, 2020
4 least Read
Developing quality applications is a complex process. To succeed, we need to use tools that support coding. These processes sometimes need automation. Due to the needs of developers and sysadmin, this process will reduce deployment time. Git is one of the most popular tools for this specific purpose. In this tutorial, let me learn about Git Hooks to increase the efficiency of a VPS project!
Git Hooks is an internal utility that can improve Git usage. Learn how it works correctly!
What is git?
Before learning about Hooks, learn about Git.
Git is an open source version control application. With this application, we can track the development of the software in detail.
Git is popular with developers and most open source projects use the platform.
What are Git Hooks?
With Git, we can create project development branches, register changes, and control absolute versions. However, you can automate this process. Git automation can operate at a programming and deployment level. That is the use of Hook.
Git hooks are script shells that run automatically before or after Git executes important commands like Commit or Push. In order for Hook to work, it needs to provide execution rights to the Unix system. By using these scripts, you will automate a few things.
Git will activate the example of Git hooks as soon as the local repository starts up. It should be noted that websites like Github or Gitlab do not allow it to be used in the first instance. Therefore they are mostly used in local or private instances
How to use Git Hooks
Hooks are stored in folders .git / hooks / of each mirrored project or in a newly created local repository. There we will find many examples of Hooks.
To activate the hook, simply create the file and save it in the directory .git / hooks /. However, the file name has been assigned to you by Git according to the Git definition as below:
Git Hook | Git command | Description |
applypatch-msg.sample | git am | When the patch notification is changed |
commit-msg.sample | git commit | To set the commit execution notification |
fsmonitor-watchman.sample | launch watchman | For watchman integration |
post-update.sample | git push | By updating all data after push |
pre-applypatch.sample | git am | Before applying the patch |
pre-commit.sample | git commit | Before committing |
prepare-commit-msg.sample | git commit | When commit message is set |
pre-push.sample | git push | Before the push |
pre-rebase.sample | git rebase | Before skipping or entering |
pre-receive.sample | git push | When we push and receive data from the remote repository |
update.sample | git push | By updating the remote data in a push By updating data remotely when push |
As we can see, every Hook carries a Git command. Thanks to it, we can calculate when it is convenient to use Hook.
Git Hooks example
Hooks' use is broad, but knowledge of Bash and other languages such as Python or Ruby is needed to take full advantage of its potential. Here are some basic examples:
Displays information about the Commit implementation
This example gives information about the commit action. Create a file named prepare-commit-msg in the directory .git / hooks / of the repository. Then write the following script:
#!/bin/sh
SOB=$(git config github.user)
grep -qs "^$SOB" "$1" || echo ". Cambio por @$SOB" >> "$1"
Then save and set the executable file.
:~$ chmod +x prepare-commit-msg
With this simple Hook, when making a commit, we will get relevant information about it right away.
Create documents when changes are uploaded
Pre-push hook allows documentation of the code if we have the builder. Every time we make changes, the document will be made automatically.
Create a pre-push in the same directory as before and add the script:
#!/bin/bash
doxygen Doxyfile
git add docs/
git commit -m "Update documentation ($(date +%F@%R))"
Save the file and finally set the execution rights.
:~$ chmod +x pre-push
Find and fix Trailing Whitespace in Commit
This type of Git Hooks pre-commit is fairly straightforward. Create a file called pre-commit and add the following:
#!/bin/bash -l
.git/hooks/pre-commit-master-no-no
if [[ $? == 1 ]]
then
exit 1
fi
.git/hooks/pre-commit-debugger
.git/hooks/pre-commit-trailing-spaces
.git/hooks/pre-commit-images
.git/hooks/pre-commit-pair
Now trailing whitespace will be found and fixed in all commits.
Conclude
Git is an important tool for programmers today. Git hooks are a great way to improve this tool even more! In this tutorial, we looked at the basic concept of automating processes using Git Hooks.
Creating scripts for Git Hooks can be troublesome if you don't have a solid knowledge of the programming language, like bash.
But now that you know what Git Hooks are, you also learn how to use basic Git Hooks first. We recommend that you follow these orthodox documents and continue to dig deeper!
0 Comments