From 65f39f7c52f6a9674fdb1bc051b7ecd7d28811fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Thu, 30 Jul 2020 17:59:22 +0200 Subject: [PATCH] Add Repository.git_repo and GitRepository.default_branch_name(), use them for util.html#breadcrumb() --- gitsrht/git.py | 7 +++++++ gitsrht/templates/utils.html | 3 +-- gitsrht/types/__init__.py | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/gitsrht/git.py b/gitsrht/git.py index d87a07f..760a941 100644 --- a/gitsrht/git.py +++ b/gitsrht/git.py @@ -60,6 +60,13 @@ class Repository(GitRepository): branch = self.branches.get(branch) return branch + def default_branch_name(self): + branch = self.default_branch() + if branch: + return branch.name[len("refs/heads/"):] + else: + return None + @property def is_empty(self): return len(list(self.branches.local)) == 0 diff --git a/gitsrht/templates/utils.html b/gitsrht/templates/utils.html index dad52be..e807750 100644 --- a/gitsrht/templates/utils.html +++ b/gitsrht/templates/utils.html @@ -1,6 +1,5 @@ {% macro breadcrumb(ref, repo, path, path_join) %} -{# TODO: Default branches other than master #} -{% if ref != "master" %} +{% if ref != repo.git_repo.default_branch_name() %} ref: {{ ref }} diff --git a/gitsrht/types/__init__.py b/gitsrht/types/__init__.py index 9a526cc..218bcbe 100644 --- a/gitsrht/types/__init__.py +++ b/gitsrht/types/__init__.py @@ -3,6 +3,7 @@ import sqlalchemy as sa from sqlalchemy import event from srht.database import Base from srht.oauth import ExternalUserMixin, ExternalOAuthTokenMixin +from gitsrht.git import Repository as GitRepository from scmsrht.repos import BaseAccessMixin, BaseRedirectMixin from scmsrht.repos import BaseRepositoryMixin, RepoVisibility @@ -19,6 +20,8 @@ class Redirect(Base, BaseRedirectMixin): pass class Repository(Base, BaseRepositoryMixin): + _git_repo = None + def update_visibility(self): if not os.path.exists(self.path): # Repo dir not initialized yet @@ -34,6 +37,12 @@ class Repository(Base, BaseRepositoryMixin): elif not should_exist and os.path.exists(path): os.unlink(path) + @property + def git_repo(self): + if not self._git_repo: + self._git_repo = GitRepository(self.path) + return self._git_repo + def update_visibility_event(mapper, connection, target): target.update_visibility() -- 2.38.4