Ignore lines running debuggers like pdb.set_trace() or debugger in the repository?

Viewed 41

Sometimes when I commit my code in python or javascript, I forgot to remove the debuggers, so I must do it manually.

I know something about creating a .gitattributes file at the root of the project with the following code:

*.py filter=import pdb; pdb.set_trace()

*.js filter=debugger

But when I commit the code, it saves the lines in the repository anyway.

How to remove or at least comment these lines in the repository, and put them back in the working directory?

2 Answers

Try commenting the unwanted lines out, in js this is done by putting the // operator before unwanted lines. In python this is done by the # operator before the unwanted line for instance

#filter=import pdb; pdb.set_trace()

And for js

//filter=debugger

Opinion: the filter option in a .gitattributes should not be used the way you're trying to use it. Even if you fix the syntax, this is a bad idea.


The actual problem is that the syntax for filter lines in .gitattributes is simply:

filter=<name>

You must then define this filter (one whose name you substitute in for the angle bracketed expression) in .git/config or your global .gitconfig. For instance, if you had a filter named hack-debug-lines-in-python, you might have:

*.py filter=hack-debug-lines-in-python

in the .gitattributes file, and then:

[filter "hack-debug-lines-in-python"]
    clean = ...
    smudge = ...

in your .git/config. You would need to fill in the three dots here with an actual program that can do the debug-line trickiness that you want.

You will need to write this filter program. Git does not include one.

Related