Skip to content

verri.git

verri.git.commit_ts(ref='HEAD')

Source code in verri/git.py
def commit_ts(ref='HEAD'):
    commit = resolve(ref)
    return dates.from_ts(int(git('show', '--quiet', '--format=%cd', '--date=unix', commit)))

verri.git.num_commits_since(ts)

Source code in verri/git.py
def num_commits_since(ts):
    commits = git('log', '--first-parent', f'--since={int(ts.timestamp())}', '--format=%H', '--date=unix').splitlines(
        keepends=False
    )

    if commits and (shallow := shallow_refs()) and commits[-1] in shallow:
        # oldest commit that is included with the --since filter is shallow, unable to check whether there *should* be
        # more commits that would match the --since filter, refuse an unclear commit count
        raise RepositoryTooShallow(commits[-1])

    return len(commits)

verri.git.clean()

Source code in verri/git.py
def clean():
    return not git('status', '--porcelain', '--untracked-files=no')

verri.git.branch()

Source code in verri/git.py
def branch():
    return git('branch', '--show-current') or environments.ci_current_branch()

verri.git.resolve(ref='HEAD')

Source code in verri/git.py
def resolve(ref='HEAD'):
    return git('rev-parse', ref)

verri.git.short(ref='HEAD')

Source code in verri/git.py
def short(ref='HEAD'):
    if _is_commit(ref):
        return ref[:7]
    else:
        return short(resolve(ref))

verri.git.shallow_refs()

Source code in verri/git.py
def shallow_refs():
    git_dir = git('rev-parse', '--git-dir')
    if (shallow := Path(git_dir) / 'shallow').exists():
        # .git/shallow exists, listing refs that are treated as shallow
        return set(shallow.read_text().splitlines(keepends=False))
    else:
        return None

verri.git.git(*args)

Source code in verri/git.py
def git(*args):
    try:
        return subprocess.check_output(('git', *args), stderr=subprocess.PIPE, text=True).strip()
    except FileNotFoundError as e:
        raise CommandError('git') from e
    except subprocess.CalledProcessError as e:
        error = e.stderr.strip() if e.stderr else None
        if e.returncode == 128 and error:
            # 128 signals a fatal error from git
            if 'not a git repository' in error:
                raise NoRepository(error) from e
            raise CommandError(error) from e
        # git might have failed for another reason, continue original error
        raise