diff --git a/libtft.py b/libtft.py index e57561f..08b6ca2 100644 --- a/libtft.py +++ b/libtft.py @@ -138,4 +138,35 @@ def repo_create(path): config = repo_default_config() config.write(f) - return repo \ No newline at end of file + return repo + +def take_parent_dir(path): + for i in range(len(path) -1, -1, -1): + if path[i] == "/": + break + + if i == 0: + if path == "/": + return None + # when we have "/*" + return "/" + + return path[:i] + +def repo_find(path = ".", force=True): + if path == ".": + path = os.path.realpath(path) + + if os.path.isdir(path + "/.git"): + return GitRepository(path) + + parent = take_parent_dir(path) + + if parent: + # parent exists + return repo_find(parent, force) + + if force: + raise Exception("No git directory.") + + return None