Git Hooks - The Magical Helpers of Your Codebase

Concept of Git Hooks explained in a playful manner

We as developers cherish bug free codes, smooth code reviews and beautiful git history and logs to look at. We can’t help but fall in love with the developer who submits clean, well-organized changelists and consistent, clear commit messages. Let's be honest, we all do. This raises a question: how to get someone fall in love with our code review changelist, pull request messages and commits. Well who doesn’t like to be loved. I absolutely do.

This is where git hooks play a part in making our code reviews fall for us without even lifting a finger. Okay enough of the praise of git hooks. Let’s dive into what git hooks are. Are they even worth this praise or am I crazy:) Intrigued? Let me a weave you a little story. Keep reading.

Meet the git hooks

Once upon a time, in the kingdom of Codeville, there was a bustling city called Git Town. This town was where all the coders lived, working day and night to create beautiful, bug-free code. Now, Git Town had some magical helpers called Git Hooks. These hooks were tiny, invisible creatures with a special job: to make sure everything ran smoothly in the land of code. Git Hooks are like the secret agents of your codebase. They jump into action at specific points in your Git workflow, ensuring everything goes according to plan. Imagine them as tiny guardians who help you catch mistakes, automate tasks, and enforce rules.

Now there are different types of these little secret agents. Let’s explore few of them with their roles in code review process by considering that every piece of code passes through various gates before it is reviewed by the wise code reviewers.

Pre-commit hook: First line of defence

  1. Purpose: The pre-commit hook is like the knight guarding the drawbridge. It stops bad code from even entering the castle courtyard.
  2. Tasks: It can run linters, check for syntax errors, ensure code adheres to style guidelines, and even run unit tests.
  3. Benefit: By catching errors early, it ensures that only code meeting a certain standard gets committed, reducing the workload for reviewers who don’t have to point out basic issues.

# Example: A pre-commit hook to run a linter

# .git/hooks/pre-commit

#!/bin/bash

echo "Running linter..."

eslint . || { echo "Linting failed! Please fix errors."; exit 1; }

Commit-Msg hook: The herald of clarity

  1. Purpose: This hook acts like a herald who ensures that every commit message is clear and informative.
  2. Tasks: It can enforce commit message formats, ensuring they include necessary details like ticket numbers or follow a specific style.
  3. Benefit: Clear commit messages help reviewers understand the context and purpose of changes, making the review process smoother.

# Example: A commit-msg hook to enforce a commit message format

# .git/hooks/commit-msg

#!/bin/bash

commit_msg=$(cat "$1")

if ! grep -qE "^[A-Z0-9-]+: .+" <<< "$commit_msg"; then

echo "Commit message must start with a ticket number followed by a colon."

exit 1

fi

Pre-push hook: The border Patrol

  1. Purpose: Before code leaves the developer’s local environment to enter the shared repository, the pre-push hook performs final checks.
  2. Tasks: It can run the full test suite or ensure the latest changes are pulled before pushing.
  3. Benefit: Ensures the integrity and consistency of the codebase by catching issues that might have been missed earlier, reducing the chances of broken builds.

# Example: A pre-push hook to run tests

# .git/hooks/pre-push

#!/bin/bash

echo "Running tests before push..."

if ! ./run_tests.sh; then

echo "Tests failed! Push aborted."

exit 1

fi

Pre-receive hook: The final gatekeeper

  1. Purpose: This hook runs on the remote repository and serves as the final gatekeeper, ensuring all incoming code meets the repository’s standards.
  2. Tasks: It can enforce policies like no direct pushes to the main branch, ensuring all changes go through a pull request (PR) process.
  3. Benefit: Maintains repository health by enforcing best practices and preventing unauthorized changes, ensuring reviewers only see code that follows the established workflow.

These hooks help reviewers to focus on more critical aspects of code like logic, functionality and design through maintaining consistency, efficiency and quality.

So after all, git hooks are the unsung heroes working behind the scenes making the kingdom of codeville a place of order and excellence. By recognizing these heroes, the developers in the kingdom live happily ever after.

If you want your code reviewer to fall in love with you ( trust me, they will ), use git hooks. You will be helping yourself as well as others in the process.

Hope you liked the story and the secret to love ;)

Please subscribe, like and share if you enjoyed the tale of our agents ( git hooks). Be honest, you would watch this movie, right?

I appreciate all the feedback and support.

Yours in discovery,

Shruti


要查看或添加评论,请登录

社区洞察