Skip to content

Instantly share code, notes, and snippets.

@fralau fralau/readme.md
Last active Apr 11, 2018

Embed
What would you like to do?
Python: know whether the current directory is part of a git repo

How to know wether current directory is part of a git repo

Idea

The git rev-parse command returns 0 if the current directory is in a git repo and an error code otherwise. It is thus necessary to call the bash command, suppressing stdout and stderr, in order to get the return code.

Code

import os, subprocess
def is_git_repo():
    "Check whether this is a git repo"
    # we use the exit code
    with open(os.devnull, "w") as d:
        return not bool(subprocess.call('git rev-parse', shell=True,
                        stdout=d, stderr=d))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.