检查我的提交是否在emacs / git中导入了pdb? [英] check if my commit has 'import pdb' in emacs/git?
问题描述
我经常提交 import pdb; pdb.set_trace()
有没有方便的方法来阻止我这样做?
我使用emacs / git(magit)。
为了完整起见,以下是如何检查索引中版本的内容,构建原始答案,还有一些更改(未被测试为完整的钩子,但应该工作):
#!/ bin / sh
has_import = false
git diff --cached --no-renames --name-status - diff-filter = AM |
同时读取st文件;
case $ $ $ $ $ $ $ $
如果git显示:$ file|
grep -E^ [^#] * \bimport [[:space:]] + pdb\b;那么
echo$ file:have import pdb
exit 1
fi ;;
esac
done || has_import = true
如果$ has_import;那么
exit 1
fi
最重要的一点是, code> git show:$ file trick,它使用 git show
从索引中提取分段版本。 >
我也:
- 添加
- 无重命名
使重命名的文件显示为A
dded(处理R
更难,可能只要将它们视为新的); - 删除
C
,因为它会触发失败(因为其他文件名是也打印,就像R
enames,但我认为这里不会触发这里); - 删除了一些bash特定的语法通过使用
case
;和 - 加强了grep表达式(仍然不完美,您可以从pdb import ... 执行
或更多可能,像
导入集合,pdb
,它不会捕获;但现在它在import
之后处理多个空格,并避免错误的命中,例如import pdbase
)。 - 每个 Matthieu Moy的评论,加强了shell片段设置
has_import
变量,您可以稍后使用。 (如果您以后不打算使用,可以直接删除该变量并直接使用exit 1
。)
(这仍然至少有一个小缺陷:提取的文件内容没有应用任何污迹过滤器,但如果您的污迹和干净的过滤器添加并删除 import
行,我怀疑没有一个预提交钩可以帮助你。:-))
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:
- added
--no-renames
to make renamed files show up asA
dded (dealing withR
is harder, might as well just treat them as new); - removed
C
as it would fail if it triggered (because the "other" file name is also printed, just as forR
enames, but I think it will not trigger here anyway); - removed some bash-specific syntax by using
case
; and - beefed up the grep expression a bit (it's still not perfect, you could do
from pdb import ...
, or more likely, something likeimport collections, pdb
, which it would not catch; but now it handles multiple spaces afterimport
, and avoids false hits on, e.g.,import pdbase
). - per Matthieu Moy's comment, beefed up the shell fragment to set a
has_import
variable you can use later. (If you don't intend to use anything later you can eliminate the variable and useexit 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. :-) )
这篇关于检查我的提交是否在emacs / git中导入了pdb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!