def coerce_repository(value, context=None):
<EOL>context = context or LocalContext()<EOL>if isinstance(value, Repository):<EOL><INDENT>return value<EOL><DEDENT>if not isinstance(value, string_types):<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg % type(value))<EOL><DEDENT>try:<EOL><INDENT>return find_configured_repository(value)<EOL><DEDENT>except NoSuchRepositoryError:<EOL><INDENT>pass<EOL><DEDENT>vcs_type, _, location = value.partition('<STR_LIT:+>')<EOL>if vcs_type and location:<EOL><INDENT>kw = {<EOL>'<STR_LIT>': context,<EOL>'<STR_LIT>' if context.exists(location) else '<STR_LIT>': location,<EOL>}<EOL>try:<EOL><INDENT>return repository_factory(vcs_type, **kw)<EOL><DEDENT>except UnknownRepositoryTypeError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>for cls in load_backends():<EOL><INDENT>if cls.contains_repository(context, value):<EOL><INDENT>return repository_factory(cls, context=context, local=value)<EOL><DEDENT><DEDENT>if value.endswith('<STR_LIT>'):<EOL><INDENT>from vcs_repo_mgr.backends.git import GitRepo<EOL>return repository_factory(GitRepo, **{<EOL>'<STR_LIT>': context,<EOL>'<STR_LIT>' if context.exists(value) else '<STR_LIT>': value,<EOL>})<EOL><DEDENT>msg = ("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL>raise ValueError(msg % value)<EOL>
Convert a string (taken to be a repository name or location) to a :class:`Repository` object.

:param value: The name or location of a repository (a string) or a
              :class:`Repository` object.
:param context: An execution context created by :mod:`executor.contexts`
                (defaults to :class:`executor.contexts.LocalContext`).
:returns: A :class:`Repository` object.
:raises: :exc:`~exceptions.ValueError` when the given value is not a string
         or a :class:`Repository` object or if the value is a string but
         doesn't match the name of any configured repository and also can't
         be parsed as the location of a repository.

The :func:`coerce_repository()` function creates :class:`Repository` objects:

1. If the value is already a :class:`Repository` object it is returned to
   the caller untouched.
2. If the value is accepted by :func:`find_configured_repository()` then
   the resulting :class:`Repository` object is returned.
3. If the value is a string that starts with a known VCS type prefix (e.g.
   ``hg+https://bitbucket.org/ianb/virtualenv``) the prefix is removed from
   the string and a :class:`Repository` object is returned:

   - If the resulting string points to an existing local directory it will
     be used to set :attr:`~Repository.local`.
   - Otherwise the resulting string is used to set
     :attr:`~Repository.remote`.
4. If the value is a string pointing to an existing local directory, the
   VCS type is inferred from the directory's contents and a
   :class:`Repository` object is returned whose :attr:`~Repository.local`
   property is set to the local directory.
5. If the value is a string that ends with ``.git`` (a common idiom for git
   repositories) a :class:`Repository` object is returned:

   - If the value points to an existing local directory it will be used to
     set :attr:`~Repository.local`.
   - Otherwise the value is used to set :attr:`~Repository.remote`.