Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 851
Ogen Avatar asked Jun 14 '16 03:06

Ogen


People also ask

How do I see my Git hooks?

The hooks are all stored in the hooks subdirectory of the Git directory. In most projects, that's . git/hooks .

Where are Git hooks executed?

Hooks can reside in either local or server-side repositories, and they are only executed in response to actions in that repository.

Are Git hooks pushed to remote?

No. Hooks are per-repository and are never pushed.

Are Git hooks tracked?

Local Git hooks reside in the . git/hooks directory and are not treated as project files. They are not tracked in the Index and because of that they are not included when someone clones a repository.


1 Answers

I was able to solve the problem using this python code. It simply 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
        break
like image 168
Ogen Avatar answered Oct 22 '22 03:10

Ogen