~edwargix/git.sr.ht

8e60f1670a276cac280694ddec289e724f2afe8a — Drew DeVault 6 years ago cbf11ba
Only fetch annotations when using cold cache
2 files changed, 19 insertions(+), 10 deletions(-)

M gitsrht/annotations.py
M gitsrht/blueprints/repo.py
M gitsrht/annotations.py => gitsrht/annotations.py +13 -6
@@ 200,16 200,23 @@ def validate_annotation(valid, anno):
                    "f'{field}' must be a string")

class AnnotatedFormatter(_BaseFormatter):
    def __init__(self, annos, link_prefix):
    def __init__(self, get_annos, link_prefix):
        super().__init__()
        self.annos = dict()
        self.get_annos = get_annos
        self.link_prefix = link_prefix
        for anno in (annos or list()):

    @property
    def annos(self):
        if hasattr(self, "_annos"):
            return self._annos
        self._annos = dict()
        for anno in (self.get_annos() or list()):
            lineno = int(anno["lineno"])
            self.annos.setdefault(lineno, list())
            self.annos[lineno].append(anno)
            self.annos[lineno] = sorted(self.annos[lineno],
            self._annos.setdefault(lineno, list())
            self._annos[lineno].append(anno)
            self._annos[lineno] = sorted(self._annos[lineno],
                    key=lambda anno: anno.get("from", -1))
        return self._annos

    def _annotate_token(self, token, colno, annos):
        # TODO: Extend this to support >1 anno per token

M gitsrht/blueprints/repo.py => gitsrht/blueprints/repo.py +6 -4
@@ 48,13 48,15 @@ def get_readme(repo, tip, link_prefix=None):
            link_prefix=link_prefix)

def _highlight_file(repo, ref, name, data, blob_id):
    annotations = redis.get(f"git.sr.ht:git:annotations:{repo.id}:{blob_id}")
    if annotations:
        annotations = json.loads(annotations.decode())
    def get_annos():
        annotations = redis.get(f"git.sr.ht:git:annotations:{repo.id}:{blob_id}")
        if annotations:
            return json.loads(annotations.decode())
        return None
    link_prefix = url_for(
        'repo.tree', owner=repo.owner, repo=repo.name, ref=ref)
    return get_highlighted_file("git.sr.ht:git", name, blob_id, data,
            formatter=AnnotatedFormatter(annotations, link_prefix))
            formatter=AnnotatedFormatter(get_annos, link_prefix))

def render_empty_repo(owner, repo):
    origin = cfg("git.sr.ht", "origin")