Пример #1
0
def get_repo_information(repo_root):
    """ Returns a dictionary with fields branch, commit, org, repo

        Raises RepoInfoException.
    """
    # print('Creating a Repo object for root %s' % repo_root)
    gitrepo = Repo(repo_root)
    try:
        try:
            branch = str(gitrepo.active_branch)
        except TypeError:
            # TypeError: HEAD is a detached symbolic reference as it points
            # to '4bcaf737955277b156a5bacdd80d1805e4b8bb25'
            branch = None

        commit = gitrepo.head.commit.hexsha
        try:
            origin = gitrepo.remotes.origin
        except AttributeError:
            raise ValueError('No remote "origin".')
        url = origin.url
    except ValueError as e:
        msg = 'Could not get branch, commit, url. Maybe the repo is not initialized.'
        raise_wrapped(RepoInfoException, e, msg, compact=True)
        raise

    # now github can use urls that do not end in '.git'
    if 'github' in url and not url.endswith('.git'):
        url += '.git'
    try:
        org, repo = org_repo_from_url(url)
    except NotImplementedError:
        org, repo = None, None

    author_name = gitrepo.head.commit.author.name
    author_email = gitrepo.head.commit.author.email
    committed_date = gitrepo.head.commit.committed_date

    # avoid expensive garbage collection
    gitrepo.git = None
    return dict(branch=branch,
                commit=commit,
                org=org,
                repo=repo,
                committed_date=committed_date,
                author_name=author_name,
                author_email=author_email)