diff --git a/inlineplz/util/git.py b/inlineplz/util/git.py index ddacb1e6..538404b1 100644 --- a/inlineplz/util/git.py +++ b/inlineplz/util/git.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- +import os import subprocess +import tempfile def current_sha(): @@ -100,3 +102,36 @@ def command(*args): return ( subprocess.check_output(git_command).strip().decode("utf-8", errors="replace") ) + + +def clone(url, dir=None, token=None, ref=None): + if not dir: + dir = os.getcwd() + if token: + # https://github.com/blog/1270-easier-builds-and-deployments-using-git-over-https-and-oauth + url = url.replace("https://", "https://{}@".format(token)) + print("Cloning: {}".format(url)) + try: + os.makedirs(dir) + except OSError: + pass + try: + subprocess.check_call(["git", "init"], cwd=dir) + + pull_cmd = ["git", "pull", url] + if ref: + pull_cmd.append(ref) + subprocess.check_call(pull_cmd, cwd=dir) + return True + except subprocess.CalledProcessError: + return False + + +def clone_dotfiles(url, org, token=None): + dotfile_dir = tempfile.mkdtemp() + for repo in ["dotfiles", ".dotfiles", ".github"]: + clone_url = "/".join([url, org, repo]) + ".git" + print("Cloning: {}".format(clone_url)) + dotfile_path = os.path.join(dotfile_dir, repo) + if clone(clone_url, dotfile_path, token): + return dotfile_path