def init_git_repo(path, no_create, bare, log): """Initialize local Git repository""" path = os.path.abspath(path) if os.path.isfile(path): raise ArchiveError("Invalid Git repo at {}: path exists but is not a " "directory".format(path)) if not os.path.isdir(path) or not os.listdir(path): if no_create: raise ArchiveError("No git repo at {}, refusing to create " "one".format(path)) if not os.path.isdir(path): try: os.mkdir(path) except (FileNotFoundError, PermissionError) as err: raise ArchiveError("Failed to mkdir {}: {}".format(path, err)) if not os.listdir(path): log.info("Initializing a new Git repo at %s", path) repo = GitRepo.init(path, bare) try: repo = GitRepo(path, is_topdir=True) except GitError: raise ArchiveError("Non-empty directory that is not a Git repository " "at {}\nPlease specify an existing Git repository, " "an empty directory or a non-existing directory " "path.".format(path)) return repo