Automating Tasks with Git Hooks

Tutorial 3 of 5

Automating Tasks with Git Hooks

1. Introduction

In this tutorial, we aim to automate tasks using Git hooks. Git hooks are scripts that Git executes before or after events such as commit, push, and receive. They are a built-in feature of Git, which makes them an ideal tool for automating tasks in your workflow, such as linting code or deploying to a server.

By the end of this tutorial, you will be able to:

  • Understand the concept of Git hooks and how they work
  • Create custom Git hooks
  • Use Git hooks to automate tasks in your development workflow

Prerequisites: Basic knowledge of Git and Bash scripting.

2. Step-by-Step Guide

Understanding Git Hooks

Git hooks reside in the .git/hooks directory of every Git repository. By default, Git provides several sample hooks in this directory.

Hooks are either designated as 'pre' or 'post' actions. A 'pre' hook is executed before the event, and if it fails (exits with a non-zero status), the event is aborted. On the other hand, a 'post' hook is executed after the successful completion of the event.

Creating a Git Hook

To create a Git hook, you need to create a file in the .git/hooks directory, give it an appropriate name related to the event it should respond to (like pre-commit, post-commit, etc.), and make it executable.

Here's an example of creating a pre-commit hook:

  1. Navigate to your Git repository:

bash cd /path/to/your/repo

  1. Create a new file in the hooks directory and name it according to the event. In this case, pre-commit:

bash touch .git/hooks/pre-commit

  1. Make the file executable:

bash chmod +x .git/hooks/pre-commit

  1. Open the file and add your script. Here's a simple Bash script that prevents committing if the word "TODO" is found in your code:

```bash
#!/bin/sh

if git grep -q "TODO" -- ':!*.md'; then
echo "Commit contains TODO, aborting commit."
exit 1
fi
```

3. Code Examples

Example 1: A pre-commit Hook for Linting

#!/bin/sh

# Run linter
npm run lint

# If linting fails, exit with non-zero status code
if [ $? -ne 0 ]; then
  echo "Lint errors found. Please fix them before committing."
  exit 1
fi

This pre-commit hook runs your project's linting script before every commit. If there are any linting errors, the commit is aborted.

Example 2: A pre-push Hook for Running Tests

#!/bin/sh

# Run tests
npm test

# If tests fail, exit with non-zero status code
if [ $? -ne 0 ]; then
  echo "Tests failed. Please fix them before pushing."
  exit 1
fi

This pre-push hook runs your project's test suite before every push. If any test fails, the push is aborted.

4. Summary

In this tutorial, we learned about Git hooks and how to use them to automate tasks in our development workflow. We learned how to create custom hooks and provided examples of hooks for linting code and running tests.

For further learning, you can explore more complex uses of Git hooks, like automating deployment or integrating with continuous integration services.

5. Practice Exercises

  1. Exercise: Create a pre-commit hook that checks for console.log statements in your code. If any are found, abort the commit.

Solution: Here's a possible solution using a Bash script:

```bash
#!/bin/sh

if git grep -q "console.log" -- ':!*.md'; then
echo "Commit contains console.log, aborting commit."
exit 1
fi
`` This script usesgit grep` to search for "console.log" in the project files. If it's found, the script exits with a non-zero status code, aborting the commit.

  1. Exercise: Create a post-commit hook that automatically pushes to the remote repository after every successful commit.

Solution: Here's a possible solution using a Bash script:

```bash
#!/bin/sh

git push origin main
```
This script automatically pushes to the 'main' branch of the 'origin' remote after every successful commit.

  1. Exercise: Create a pre-push hook that runs your project's build script before every push. If the build fails, abort the push.

Solution: Here's a possible solution using a Bash script:

```bash
#!/bin/sh

# Run build script
npm run build

# If build fails, exit with non-zero status code
if [ $? -ne 0 ]; then
echo "Build failed. Please fix the issues before pushing."
exit 1
fi
```
This script runs your project's build script before every push. If the build fails, the script exits with a non-zero status code, aborting the push.

Remember to test your Git hooks thoroughly to ensure they behave as expected. Happy coding!