How to Install and Use Pre-commit on Ubuntu WSL 2

Pre-commit is a framework for managing and maintaining multi-language pre-commit hooks. It helps developers catch issues before they are committed to version control. This is important because it can improve code quality and save time by preventing errors from making it into the main codebase. In this guide, we will show you how to install and configure Pre-commit on Ubuntu running on Windows Subsystem for Linux (WSL) 2.

Scope

This guide covers the following topics:

  • Installing Pre-commit on Ubuntu WSL 2
  • Adding a Pre-commit configuration
  • Installing Git hook scripts
  • Running Pre-commit checks

Purpose

The purpose of this guide is to provide a step-by-step approach to set up Pre-commit on your Ubuntu WSL 2 environment. This will ensure you have a smooth coding experience and maintain high code quality.

Installation Steps

Step 1: Install Pre-commit

To install Pre-commit, open your terminal and run the following command:

pip install pre-commit

You can find the latest release of Pre-commit here.

Step 2: Validate the Installation

After installation, you can verify the version of Pre-commit by running:

pre-commit --version

You should see output like this:

pre-commit 4.0.1

Step 3: Add a Pre-commit Configuration

Create a configuration file named .pre-commit-config.yaml in your project directory. You can add the following content to this file:

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.3.0
    hooks:
    -   id: check-yaml
    -   id: end-of-file-fixer
    -   id: trailing-whitespace
-   repo: https://github.com/psf/black
    rev: 22.10.0
    hooks:
    -   id: black

You can generate a basic configuration using the command:

pre-commit sample-config > .pre-commit-config.yaml

For more information on available hooks, visit more hooks

Step 4: Install the Git Hook Scripts

Run the following command to set up the Git hook scripts:

pre-commit install

You should see:

pre-commit installed at .git/hooks/pre-commit

Step 5: Run Pre-commit Checks

To run the pre-commit hooks on all files, use this command:

pre-commit run --all-files

# Optional
pre-commit run --files test.py

Troubleshooting Issue

If you encounter an error like:

git: 'remote-https' is not a git command. See 'git --help'.

To check if git-remote-https exists in your Git installation directory, run:

ls $(git --exec-path)

If git-remote-https is not present, your Git installation may be incomplete. To fix it, you can reinstall Git with the following commands:

sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git

After reinstalling, check your Git version again:

git --version

Finally, try running the pre-commit command again:

pre-commit run --all-files

# Optional
pre-commit run --files test.py

Incase, you want to disabled the pre-commit hooks, you can disable it manually by renaming the pre-commit hook file in the .git/hooks directory:

mv .git/hooks/pre-commit .git/hooks/pre-commit.disabled

This will effectively disable the hook. To re-enable it, simply rename it back to pre-commit.

You can visit this guide on how to utilize pre-commit with Flake8 and Black tools.

Final Thoughts

Installing and using Pre-commit on Ubuntu WSL 2 is straightforward and significantly enhances your coding workflow. By setting up pre-commit hooks, you can ensure that your code meets quality standards before it gets committed to your repository. If you encounter issues, check the installation steps and refer to the Pre-commit documentation for additional support.

For more information on available hooks and features, visit Pre-commit.com.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.