def identify_revision(repository, treeish):
    # try to find it from the database first
    if len(treeish) == 40:
        revision = Revision.query.filter(
            Revision.repository_id == repository.id,
            Revision.sha == treeish,
        ).first()
        if revision:
            return revision

    vcs = repository.get_vcs()
    if not vcs:
        return

    try:
        commit = vcs.log(parent=treeish, limit=1).next()
    except CommandError:
        # update and try one more time
        try:
            vcs.update()
        except ConcurrentUpdateError:
            # Retry once if it was already updating.
            vcs.update()
        try:
            commit = vcs.log(parent=treeish, limit=1).next()
        except CommandError:
            # TODO(dcramer): it's possible to DOS the endpoint by passing invalid
            # commits so we should really cache the failed lookups
            raise MissingRevision('Unable to find revision %s' % (treeish,))

    revision, _, __ = commit.save(repository)

    return revision
Attempt to transform a a commit-like reference into a valid revision.
dataset/ETHPy150Open dropbox/changes/changes/api/build_index.py/identify_revision