# HG changeset patch # User Martin Bornhold # Date 2016-07-21 07:00:53 # Node ID b7e26a5fe6fb8b5e28151cd9a09fbc5b152b5e6e # Parent 930b0a4d7a2571b3f8bc1ca23845582c7b497b87 vcs: Refactor the get_repo function. Goal was to not use the search_path_up argument of get_scm and include some logic that sits in model.db.Repository in get_scm_instance method. diff --git a/rhodecode/lib/vcs/__init__.py b/rhodecode/lib/vcs/__init__.py --- a/rhodecode/lib/vcs/__init__.py +++ b/rhodecode/lib/vcs/__init__.py @@ -29,7 +29,7 @@ VERSION = (0, 5, 0, 'dev') __version__ = '.'.join((str(each) for each in VERSION[:4])) __all__ = [ - 'get_version', 'get_repo', 'get_backend', + 'get_version', 'get_vcs_instance', 'get_backend', 'VCSError', 'RepositoryError', 'CommitError' ] @@ -44,7 +44,7 @@ import Pyro4 from Pyro4.errors import CommunicationError from rhodecode.lib.vcs.conf import settings -from rhodecode.lib.vcs.backends import get_repo, get_backend +from rhodecode.lib.vcs.backends import get_vcs_instance, get_backend from rhodecode.lib.vcs.exceptions import ( VCSError, RepositoryError, CommitError) diff --git a/rhodecode/lib/vcs/backends/__init__.py b/rhodecode/lib/vcs/backends/__init__.py --- a/rhodecode/lib/vcs/backends/__init__.py +++ b/rhodecode/lib/vcs/backends/__init__.py @@ -22,7 +22,8 @@ VCS Backends module """ -import os +import logging + from pprint import pformat from rhodecode.lib.vcs.conf import settings @@ -31,31 +32,29 @@ from rhodecode.lib.vcs.utils.helpers imp from rhodecode.lib.vcs.utils.imports import import_class -def get_repo(path=None, alias=None, create=False): +log = logging.getLogger(__name__) + + +def get_vcs_instance(repo_path, *args, **kwargs): """ - Returns ``Repository`` object of type linked with given ``alias`` at - the specified ``path``. If ``alias`` is not given it will try to guess it - using get_scm method + Given a path to a repository an instance of the corresponding vcs backend + repository class is created and returned. If no repository can be found + for the path it returns None. Arguments and keyword arguments are passed + to the vcs backend repository class. """ - if create: - if not (path or alias): - raise TypeError( - "If create is specified, we need path and scm type") - return get_backend(alias)(path, create=True) - if path is None: - path = os.path.abspath(os.path.curdir) try: - scm, path = get_scm(path, search_path_up=True) - path = os.path.abspath(path) - alias = scm + vcs_alias = get_scm(repo_path)[0] + log.debug( + 'Creating instance of %s repository from %s', vcs_alias, repo_path) + backend = get_backend(vcs_alias) except VCSError: - raise VCSError("No scm found at %s" % path) - if alias is None: - alias = get_scm(path)[0] + log.exception( + 'Perhaps this repository is in db and not in ' + 'filesystem run rescan repositories with ' + '"destroy old data" option from admin panel') + return None - backend = get_backend(alias) - repo = backend(path, create=create) - return repo + return backend(repo_path=repo_path, *args, **kwargs) def get_backend(alias):