When we work for several hours and get tired, it is possible to push the project quickly and not check whether the project will be built or not. In Git, it is possible to run a series of commands before pushing. Therefore, we can use this feature to build the project before pushing. For this, you must open the hooks folder in the .git folder and create a file called pre-push. Then you have to write the command related to building your project in the created file. With this, the commands in the pre-push file are executed before pushing the branches each time. For example, in the code below, we have written an example of building the project before pushing the branches.
#!/bin/sh
dotnet clean ".\GitHookExample.ConsoleApp\GitHookExample.ConsoleApp.csproj"
dotnet build ".\GitHookExample.ConsoleApp\GitHookExample.ConsoleApp.csproj"
Now, if you push your project, clean and build commands will be executed before pushing, and if all the steps are executed successfully, then the pushing operation will take place.
There are several sample files in the hooks folder:
- pre-commit
- pre-push
- pre-merge
- pre-rebase
- pre-receive
- pre-applypatch
- pre-merge-commit
- update
- push-to-checkout
- prepare-commit-msg
- post-update
- commit-msg
- fsmonitor-watchman
- applypatch-msg
In each of these files, the execution time of the commands is written. For example, the following information is written in the commit-msg file:
#!/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" "$1" || echo "$SOB" >> "$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
}
The commit-msg file is executed when you enter the git commit command. In this file, you have access to the text entered as a commit message and you can enter a series of rules to enter the commit message so that all project programmers use a specific structure to write the commit message. For example, write a pattern in the code below. Note that the programmer's commit message should start with GitHookExample and contain a number:
#!/bin/sh
#!/usr/bin/env bash
INPUT_FILE=$1
START_LINE=`head -n1 $INPUT_FILE`
PATTERN="^(GitHookExample)-[[:digit:]]+: "
if ! [[ "$START_LINE" =~ $PATTERN ]]; then
echo "Bad commit message, see example: GitHookExample-123: commit message"
exit 1
fi
In the code above, if the programmer's commit message does not match the introduced pattern, he will receive the following error at the time of commit:
Bad commit message, see example: GitHookExample-123: commit message
But if they enter a valid text:
git commit -m "GitHookExample-001: initial project"
Their changes will be successfully committed.
You can also enter the dotnet test command before pushing and merging so that before pushing and merging, all the tests will be executed first, and then the desired operation will be executed.
By default, all the files in the hooks folder have the .sample extension. To activate any of the items mentioned above, you must delete the .sample part from the file names in the hooks folder. for example:
pre-push
pre-rebase
pre-receive
pre-commit
;)
Nice article! But when I try this out (on the pre-commit hook), my project doesn't seem to get cleaned and rebuild. I've checked my core.hooksPath as suggested by this stack overflow post (https://stackoverflow.com/questions/49912695/git-pre-and-post-commit-hooks-not-running). What else can I try to investigate why my pre commit hook doesn't run? (I'm on windows)
Posted at Tuesday, April 18, 2023I use the same pre-push file content for pre-commit file and it works successfully. are you sure created file name is pre-commit ?
Posted at Tuesday, April 18, 2023