Skip to content

Instantly share code, notes, and snippets.

@DGrady
Created January 30, 2018 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DGrady/32db5223b956fece094292775e5dfd1d to your computer and use it in GitHub Desktop.
Save DGrady/32db5223b956fece094292775e5dfd1d to your computer and use it in GitHub Desktop.
Python snippet to find the nearest parent directory containing .git
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