Define client level git hooks and their implementation.

Devops Devops in Devops . 3 years ago

  3   0   0   0   0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Git hooks are the instruction scripts that gets triggered before(pre) or post(after) certain actions or events such as a git command run.

  • Git hooks can be implemented on both client(local machine) and server(remote) repositories.
  • Hooks objects are stored under .git/hooks directory. Hooks scripts are written in shell script and made executable.
  • --Code snippet of a ‘pre-commit’ script that stops the commit if a file has been deleted from the project:

#!/bin/sh #Library includes: . .git/hooks/hooks_library.lib # An example hook script to verify what is about to be committed. # Called by "git commit" with no arguments.  The hook should # exit with non-zero status after issuing an appropriate message if # it wants to stop the commit. #Aim:Check for any Deleted file in the staging area, if any it stops you from commiting this snapshot. set_variables 1 $0 if [ "$(git status --short | grep '^D')" ];then         ECHO "WARNING!!! Aborting the commit. FOUND Deleted files in the Staging area.\n" | tee -a $LOGFILE         echo "`git status --short | grep '^D' | awk -F' ' '{PRINT $2}'`\n" | tee -a $LOGFILE         exit 1; else         echo "[OK]: No deleted files, proceed to commit." | tee -a $LOGFILE         exit 0; fi

Scenario how I implemented the hooks scripts to enforce certain pre-commit and post-commit test cases:

Step 1: Running .git/hooks/pre-commit script.

[OK]: No deleted files, proceed to commit. Thu Feb  7 12:10:02 CET 2019 --------------------------------------------

Step 2: Running .git/hooks/prepare-commit-msg script.

Get hooks scripts while cloning the repo.  ISSUE#7092 Enter your commit message here. README code/install_hooks.sh code/runTests.sh database.log hooksScripts/commit-msg hooksScripts/hooks_library.lib hooksScripts/post-commit hooksScripts/pre-commit hooksScripts/pre-rebase hooksScripts/prepare-commit-msg newFile Thu Feb  7 12:10:02 CET 2019 --------------------------------------------

Step 3: Running .git/hooks/commit-msg script.

[OK]: Commit message has an ISSUE number Thu Feb  7 12:10:02 CET 2019 --------------------------------------------

Step 4: Running .git/hooks/post-commit script.

New commit made:

1c705d3 Get hooks scripts while cloning the repo. ISSUE#7092
  • A ‘pre-rebase’ script to stop the rebase on a ‘master’ branch:
  • Rebase ‘topic’ branch on ‘master’ branch

hooksProj [dev] $git rebase master topic WARNING!!! upstream branch is master. You are not allowed to rebase on master The pre-rebase hook refused to rebase.

A code snippet demonstrating the use of a ‘pre-receive’ hook that is triggered just before a ‘push’ request on the Server, can be written to reject or allow the push operation.

localRepo [dev] $git push Enumerating objects: 3, DONE. Counting objects: 100% (3/3), done. Writing objects: 100% (2/2), 272 bytes | 272.00 KiB/s, done. Total 2 (delta 0), reused 0 (delta 0) remote: pre-recieve hook script remote: hooks/pre-receive: [NOK]- Abort the push command remote: To /Users/Divya1/OneDrive/gitRepos/remoteRepo/ ! [remote rejected] dev -> dev (pre-receive hook declined) error: failed to push some refs to '/Users/Divya1/OneDrive/gitRepos/remoteRepo/'

Posted on 31 May 2022, this text provides information on Devops related to Devops in Devops. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Tuteehub forum answer Answers

Post Answer

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.