diff --git a/libwyag.py b/libwyag.py index c874e2f..35e0483 100644 --- a/libwyag.py +++ b/libwyag.py @@ -146,3 +146,42 @@ argsp.add_argument("path", def cmd_init(args): repo_create(args.path) + +def repo_find(path=".", required=True): + path = os.path.realpath(path) + + if os.path.isdir(os.path.join(path, ".git")): + return GitRepository(path) + + # If we haven't returned, recurse in parent + parent = os.path.realpath(os.path.join(path, "..")) + + match os.name: + case "nt": + is_root = os.path.splitdrive(path)[1] == "\\" + case "posix": + # If parent==path, then path is root. + is_root = parent == path + case _: + raise Exception(f"Unsupported os {os.name}") + + if is_root: + if required: + raise Exception("No git directory.") + else: + return None + + # Recursive case + return repo_find(parent, required) + +argsp = argsubparsers.add_parser("status", help="Get the status of the repo") + +argsp.add_argument("path", + metavar="directory", + nargs="?", + default=".", + help="Which directory") + +def cmd_status(args): + # TODO: actually get status + print(repo_find(args.path).worktree)