In this lesson, you will learn:

  • what pre-commit is
  • how to trigger our code quality tools at the point of a git commit
  • how to install and configure pre-commit.
You can find the scripts and code for this course within the Automating Python Code Quality repo.

Introduction

Rather than having to remember to manually run our code quality tools, we can use a tool called pre-commit.

What is pre-commit?

pre-commit is a tool that allows you to trigger different tasks and actions against your code at the point when you perform a git commit. The key benefit of pre-commit is that it prevents you from forgetting to run the required actions against your code. Not only this, but it allows you to share your pre-commit configuration to ensure the same tools and actions are being performed against your codebase.

How Does pre-commit Work?

pre-commit works by leveraging git hooks. Git hooks provide us with the ability to run a series of different shell commands at different trigger points within Git (such as push, merge, commit). These hooks are configured via files within the .git/hooks directory (shown below):

.git/hooks
├── applypatch-msg.sample
├── commit-msg.sample
├── fsmonitor-watchman.sample
├── post-update.sample
├── pre-applypatch.sample
├── pre-commit.sample
├── pre-merge-commit.sample
├── pre-push.sample
├── pre-rebase.sample
├── pre-receive.sample
├── prepare-commit-msg.sample
└── update.sample
Ready to Master Network Automation? Start Your Journey Today!
Our membership provides:
  • Full deep-dive course library (inc. Batfish, pyATS, Netmiko)
  • Code repositories inc. full course code, scripts and examples
  • 24x7 multi-vendor labs (Arista, Cisco, Juniper)
  • Private online community
  • Live monthly tech sessions
  • Access to tech session library

Join Now ➜
Close You've successfully subscribed to Packet Coders.
Close Success! Your account is fully activated, you now have access to all content.
Close Welcome back! You've successfully signed in.
Close Nearly there! To activate your account, please click the link in the email we just sent you.