changeset 8730:aee4c451566f

vcs: cleanup around get_backend and get_repo
author Mads Kiilerich <mads@kiilerich.com>
date Sun, 01 Nov 2020 06:18:48 +0100
parents 912563696e9e
children fa0a18dc0fd5
files kallithea/lib/vcs/backends/__init__.py kallithea/lib/vcs/backends/base.py kallithea/lib/vcs/backends/git/repository.py kallithea/model/db.py kallithea/model/scm.py
diffstat 5 files changed, 12 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/vcs/backends/__init__.py	Sat Oct 31 16:56:12 2020 +0100
+++ b/kallithea/lib/vcs/backends/__init__.py	Sun Nov 01 06:18:48 2020 +0100
@@ -17,7 +17,7 @@
 from kallithea.lib.vcs.utils.paths import abspath
 
 
-def get_repo(path=None, alias=None, create=False):
+def get_repo(path=None, alias=None, create=False, baseui=None):
     """
     Returns ``Repository`` object of type linked with given ``alias`` at
     the specified ``path``. If ``alias`` is not given it will try to guess it
@@ -39,7 +39,7 @@
         alias = get_scm(path)[0]
 
     backend = get_backend(alias)
-    repo = backend(path, create=create)
+    repo = backend(path, create=create, baseui=baseui)
     return repo
 
 
--- a/kallithea/lib/vcs/backends/base.py	Sat Oct 31 16:56:12 2020 +0100
+++ b/kallithea/lib/vcs/backends/base.py	Sun Nov 01 06:18:48 2020 +0100
@@ -1026,7 +1026,7 @@
 
     @LazyProperty
     def branches(self):
-        return [get_backend(self.alias).DEFAULT_BRANCH_NAME]
+        return [self.branch]
 
     @LazyProperty
     def short_id(self):
--- a/kallithea/lib/vcs/backends/git/repository.py	Sat Oct 31 16:56:12 2020 +0100
+++ b/kallithea/lib/vcs/backends/git/repository.py	Sun Nov 01 06:18:48 2020 +0100
@@ -52,8 +52,8 @@
     scm = 'git'
 
     def __init__(self, repo_path, create=False, src_url=None,
-                 update_after_clone=False, bare=False):
-
+                 update_after_clone=False, bare=False, baseui=None):
+        baseui  # unused
         self.path = abspath(repo_path)
         self.repo = self._get_repo(create, src_url, update_after_clone, bare)
         self.bare = self.repo.bare
--- a/kallithea/model/db.py	Sat Oct 31 16:56:12 2020 +0100
+++ b/kallithea/model/db.py	Sun Nov 01 06:18:48 2020 +0100
@@ -48,10 +48,9 @@
 from kallithea.lib import ext_json, ssh, webutils
 from kallithea.lib.exceptions import DefaultUserException
 from kallithea.lib.utils2 import asbool, ascii_bytes, aslist, get_changeset_safe, get_clone_url, remove_prefix, safe_bytes, safe_int, safe_str, urlreadable
-from kallithea.lib.vcs import get_backend, get_repo
+from kallithea.lib.vcs import get_repo
 from kallithea.lib.vcs.backends.base import BaseChangeset, EmptyChangeset
 from kallithea.lib.vcs.utils import author_email, author_name
-from kallithea.lib.vcs.utils.helpers import get_scm
 from kallithea.model import meta
 
 
@@ -1331,16 +1330,8 @@
 
     def scm_instance_no_cache(self):
         repo_full_path = self.repo_full_path
-        alias = get_scm(repo_full_path)[0]
-        log.debug('Creating instance of %s repository from %s',
-                  alias, self.repo_full_path)
-        backend = get_backend(alias)
-
-        if alias == 'hg':
-            self._scm_instance = backend(repo_full_path, create=False, baseui=self._ui)
-        else:
-            self._scm_instance = backend(repo_full_path, create=False)
-
+        log.debug('Creating instance of repository at %s', repo_full_path)
+        self._scm_instance = get_repo(repo_full_path, baseui=self._ui)
         return self._scm_instance
 
     def __json__(self):
--- a/kallithea/model/scm.py	Sat Oct 31 16:56:12 2020 +0100
+++ b/kallithea/model/scm.py	Sun Nov 01 06:18:48 2020 +0100
@@ -41,9 +41,9 @@
 from kallithea.lib.hooks import process_pushed_raw_ids
 from kallithea.lib.utils import action_logger, get_filesystem_repos, make_ui
 from kallithea.lib.utils2 import safe_bytes, set_hook_environment
-from kallithea.lib.vcs import get_backend
+from kallithea.lib.vcs import get_repo
 from kallithea.lib.vcs.backends.base import EmptyChangeset
-from kallithea.lib.vcs.exceptions import RepositoryError
+from kallithea.lib.vcs.exceptions import RepositoryError, VCSError
 from kallithea.lib.vcs.nodes import FileNode
 from kallithea.lib.vcs.utils.lazy import LazyProperty
 from kallithea.model import db, meta
@@ -184,15 +184,8 @@
                     raise RepositoryError('Duplicate repository name %s '
                                           'found in %s' % (name, path))
                 else:
-
-                    klass = get_backend(path[0])
-
-                    if path[0] == 'hg' and path[0] in kallithea.BACKENDS:
-                        repos[name] = klass(path[1], baseui=baseui)
-
-                    if path[0] == 'git' and path[0] in kallithea.BACKENDS:
-                        repos[name] = klass(path[1])
-            except OSError:
+                    repos[name] = get_repo(path[1], baseui=baseui)
+            except (OSError, VCSError):
                 continue
         log.debug('found %s paths with repositories', len(repos))
         return repos