Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
hubboss / boss / git_utils.py
Size: Mime:
import os
import shutil

from git import Repo


def get_repo_dir(git_url, repo_location):
    repo_dir = git_url.replace(".git", "").split("/")[1]
    return os.path.join(repo_location, repo_dir)


def repo_exist(repo_dir):
    if os.path.isdir(repo_dir):
        if os.path.isdir(os.path.join(repo_dir, '.git')):
            return True
        else:
            # directory exists, but missing .git dir, purge
            shutil.rmtree(repo_dir)
    return False


def clone_repo(git_url, repo_dir):
    # git@github.com:docker/docker-index.git
    os.makedirs(repo_dir)
    print("Cloning repo: {} to `{}`".format(git_url, repo_dir))
    return Repo.clone_from(git_url, repo_dir)


def get_working_repo(git_url, repo_location):
    repo_dir = get_repo_dir(git_url, repo_location)
    if not repo_exist(repo_dir):
        repo = clone_repo(git_url, repo_dir)
    else:
        repo = Repo(repo_dir)
        # ensure we're on the master branch
        repo.heads.master.checkout()
        remote = repo.remote()
        remote.pull()
        # Pull branches
        remote.fetch('+refs/heads/*:refs/remotes/{}/*'.format(remote.name))
        # Pull tags
        remote.fetch('+refs/tags/*:refs/tags/*')

    return repo


def get_tag(repo, tag):
    tag = "refs/tags/v{}".format(tag)
    return repo.tag(tag)