Created
January 30, 2018 23:23
-
-
Save DGrady/32db5223b956fece094292775e5dfd1d to your computer and use it in GitHub Desktop.
Python snippet to find the nearest parent directory containing .git
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cytoolz.curried as tz | |
from pathlib import Path | |
def find_project_dir(here: Path = None) -> Path: | |
""" | |
Get the path to the project directory | |
“Project directory” means the nearest parent directory of the | |
current directory that contains a `.git` directory. If there | |
is no such directory, returns this directory. | |
""" | |
if here is None: | |
here = Path() # Current directory | |
# Get the full path | |
here = here.resolve(strict=True) | |
try: | |
result = tz.pipe( | |
here.parents, | |
tz.cons(here), | |
tz.filter(lambda p: p.joinpath('.git').exists()), | |
tz.first, | |
) | |
except StopIteration: | |
result = here | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment