from zeus.config import redis
from zeus.exceptions import UnknownRepositoryBackend
from zeus.models import Repository, Revision
from zeus.vcs.base import UnknownRevision


def identify_revision(
    repository: Repository, ref: str, with_vcs: bool = True
) -> Revision:
    """
    Attempt to transform a a commit-like reference into a valid revision.
    """
    # try to find it from the database first
    if len(ref) == 40:
        revision = Revision.query.filter(
            Revision.repository_id == repository.id, Revision.sha == ref
        ).first()
        if revision:
            return revision

    if not with_vcs:
        raise UnknownRevision

    try:
        vcs = repository.get_vcs()
    except UnknownRepositoryBackend:
        raise UnknownRevision

    vcs.ensure(update_if_exists=False)

    lock_key = "sync_repo:{repo_id}".format(repo_id=repository.id)
    # lock this update to avoild piling up duplicate fetch/save calls
    with redis.lock(lock_key, expire=30):
        try:
            commit = next(vcs.log(parent=ref, limit=1))
        except UnknownRevision:
            vcs.update()
            commit = next(vcs.log(parent=ref, limit=1))

        revision, _ = commit.save(repository)

    return revision

@@ -1,34 +1,4 @@
-from zeus.config import redis%0A
 from
@@ -786,200 +786,13 @@
     
-lock_key = %22sync_repo:%7Brepo_id%7D%22.format(repo_id=repository.id)%0A    # lock this update to avoild piling up duplicate fetch/save calls%0A    with redis.lock(lock_key, expire=30):%0A        try:%0A    
+try:%0A
     
@@ -839,20 +839,16 @@
 mit=1))%0A
-    
     exce
@@ -875,20 +875,16 @@
         
-    
 vcs.upda
@@ -888,20 +888,16 @@
 pdate()%0A
-    
         
@@ -941,20 +941,16 @@
 it=1))%0A%0A
-    
     revi

Remove lock on identify_revision