I commit import pdb;pdb.set_trace()
quite often.
Is there a convenient way of preventing me doing it?
I use emacs/git (magit).
I commit import pdb;pdb.set_trace()
quite often.
Is there a convenient way of preventing me doing it?
I use emacs/git (magit).
For completeness, here's how to examine the contents of the version in the index, building off eugene's answer and with a few more changes (not tested as a complete hook, but should work):
#!/bin/sh
has_import=false
git diff --cached --no-renames --name-status --diff-filter=AM |
while read st file; do
case "$file" in
*.py)
if git show ":$file" |
grep -E "^[^#]*\bimport[[:space:]]+pdb\b"; then
echo "$file: has import pdb"
exit 1
fi;;
esac
done || has_import=true
if $has_import; then
exit 1
fi
The most important bit of change is the git show ":$file"
trick, which uses git show
to extract the staged version from the index.
I also:
--no-renames
to make renamed files show up as A
dded (dealing with R
is harder, might as well just treat them as new);C
as it would fail if it triggered (because the "other" file name is also printed, just as for R
enames, but I think it will not trigger here anyway);case
; andfrom pdb import ...
, or more likely, something like import collections, pdb
, which it would not catch; but now it handles multiple spaces after import
, and avoids false hits on, e.g., import pdbase
).has_import
variable you can use later. (If you don't intend to use anything later you can eliminate the variable and use exit 1
there directly, as he suggested.)(This still has at least one minor flaw: the extracted file-contents do not have any smudge filters applied. But if your smudge and clean filters add and remove import
lines, I suspect there's nothing a pre-commit hook can to do help you. :-) )
You can create .git/hooks/pre-commit
#!/bin/bash
git diff --cached --name-status --diff-filter=ACM | while read st file; do
if [[ "$file" =~ .py$ ]] && grep "^[^#]*import pdb" "$file"; then
echo "$file: has import pdb"
exit 1
fi
done
I just made it up. not sure if it's good enough for general use but works for me.
Thanks David
python3 -m pip install pre-commit
(or use pipx)
cd my_repo
Create a file called .pre-commit-config.yaml
with the following contents
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: debug-statements
Run pre-commit install
The next time you run git commit
it will fail with
(random) mark@DESKTOP:~/pytest-bdd$ git commit -am "Adding breakpoint"
[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
Debug Statements (Python)................................................Failed
- hook id: debug-statements
- exit code: 1
setup.py:2:0 - pdb imported
And it will not allow you to commit until
breakpoint()
or import pdb
is removed from the commit.
Note: if you just remove import pdb
but not pdb.set_trace()
e.g if you're in a rush and forget, it wont complain - but instead you now have introduced a syntax error.
See https://pre-commit.com for more information
See https://pre-commit.com/hooks.html for more hooks