Ogen Ogen -5 years ago 114
Python Question

How to know if a git hook is being called from not within the terminal/command line

I have a git hook that is running whenever some one commits to their local repository.

Some people are committing from the terminal and some people are committing from SourceTree or SmartGit or some other 3rd party application.

SourceTree behaves differently when it comes to hooks. For example, errors are red by default, and user input doesn't seem to be supported so I need to change my python scripts depending on whether or not the user is committing from SourceTree or SmartGit etc.

Is there any way to do this within my script?

Answer Source

I was able to solve the problem using this python code. It simple checks the environment variables for any occurrences of third party git clients. I don't know if it's the best solution or if it will work all the time - but it meets my needs for now.

is_terminal = True

for key in os.environ:
    if "SourceTree" in os.environ[key] or "SmartGit" in os.environ[key]:
        is_terminal = False
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download