Pre-commit hooks setup
Overview
A pre-commit hook is an automatic check that runs right before you save a commit in Git. Imagine a security guard at a mall entrance. They check your bag before you walk in. If you have something banned, they stop you. A pre-commit hook is a security guard for your code. If your code has messy formatting or errors, the hook stops the commit. ```python # This is the code you want to commit. def get_marks( student_name ): return 85 ``` ```python # When you run 'git commit', a hook like 'black' runs automatically. # It fixes the spaces before it allows the commit. def get_marks(student_name): return 85 ``` The rule is: **pre-commit hooks catch sloppy mistakes before they reach the main codebase.** You set them up once using a `.pre-commit-config.yaml` file, and they run automatically every time you commit.
It stops you from pushing code with extra spaces, missing imports, or syntax errors. This keeps the whole team's code clean without anyone having to remember the rules manually.
Where used: Git repositories, Python backend projects
Why learn this
- You will not push embarrassing formatting errors to your team's code.
- You save time because the computer fixes spaces and quotes for you automatically.
Code walkthrough
def check_commit(code):
if ' ' in code:
print('Failed: Extra spaces found')
return False
print('Passed: Committing code')
return True
bad_code = 'def get_marks(): pass'
check_commit(bad_code)
Focus: if ' ' in code:
Aha moment
code = 'def get_marks():\n return 85'
def hook(c):
return c.replace(' ', ' ')
fixed = hook(code)
print(fixed == code)
Prediction: What does this simulator print when the hook modifies the code?
Common guess: True
The hook changes the code format, making the fixed code different from the original. This is why you must stage the changes again.
Common mistakes
- Forgetting to install the hooks: You write the config file but forget to run `pre-commit install`. The checks never run when you commit.
- Committing again without adding fixes: A hook fixes a file but leaves it unstaged. You must `git add` the file again before you run git commit.
Recall questions
- Explain what a pre-commit hook is in your own words, as if to a classmate.
- What must you do if a pre-commit hook finds a formatting mistake and fixes it?
Understanding checks
A teammate says, 'I put black in my pre-commit config, so it will automatically format and push my code to GitHub.' What is wrong with this?
Pre-commit only formats the code locally and stops the commit. It never pushes code to GitHub.
Pre-commit hooks catch sloppy mistakes before they reach the main codebase. They run locally on your machine during the commit step, not during the push step.
You run git commit. The pre-commit hook fails, but it says 'files were modified by this hook'. You immediately type git commit again. What happens?
The hook fails again because you did not run git add on the modified files.
When a hook fixes a file, it modifies your working directory. You must stage those changes with git add before the commit can see them.
Practice tasks
Add a strict check
Given this simulated pre-commit hook, change it so that it fails (returns False) if the code contains the word 'print'.
Challenge
Write a simulated hook
Write a Python function called `check_code(code_string)` that acts as a simple hook. It should return True if the code is safe, and False if it contains the word 'breakpoint'.
Continue learning
Previous: os.environ and Environment Variables