~edwargix/git.sr.ht

fc809ba1219abc8ffb9afc17c17c76002b9867dc — Drew DeVault 5 years ago d20b49f
Treat transient redis failures as cache misses

This removes some long-broken code as well which would have depended on
Redis if that code path were still in use.
3 files changed, 0 insertions(+), 43 deletions(-)

M gitsrht/blueprints/api/porcelain.py
M gitsrht/blueprints/repo.py
M gitsrht/git.py
M gitsrht/blueprints/api/porcelain.py => gitsrht/blueprints/api/porcelain.py +0 -1
@@ 15,7 15,6 @@ from scmsrht.blueprints.api import get_user, get_repo
from srht.api import paginated_response
from srht.database import db
from srht.oauth import current_token, oauth
from srht.redis import redis
from srht.validation import Validation

porcelain = Blueprint("api.porcelain", __name__)

M gitsrht/blueprints/repo.py => gitsrht/blueprints/repo.py +0 -1
@@ 23,7 23,6 @@ from scmsrht.formatting import get_formatted_readme, get_highlighted_file
from scmsrht.urls import get_clone_urls
from srht.config import cfg, get_origin
from srht.markdown import markdown
from srht.redis import redis
from urllib.parse import urlparse

repo = Blueprint('repo', __name__)

M gitsrht/git.py => gitsrht/git.py +0 -41
@@ 2,7 2,6 @@ from collections import deque
from datetime import datetime, timedelta, timezone
from pygit2 import Repository as GitRepository, Tag
from jinja2 import Markup, escape
from srht.redis import redis
from stat import filemode
import pygit2
import json


@@ 117,46 116,6 @@ class AnnotatedTreeEntry:
def annotate_tree(repo, tree, commit):
    return [AnnotatedTreeEntry(repo, entry).fetch_blob() for entry in tree]

    # TODO: This is slow and broken
    key = f"git.sr.ht:git:tree:{tree.id.hex}"
    cache = redis.get(key)
    if cache:
        try:
            cache = json.loads(cache.decode())
            return [AnnotatedTreeEntry.deserialize(
                e, repo).fetch_blob() for e in cache.values()]
        except:
            redis.delete(key)

    tree = { entry.id.hex: AnnotatedTreeEntry(
        repo, entry) for entry in tree }

    parents = deque(commit.parents)
    left_tree = set(v for v in tree.values())
    unfinished = set(left_tree)
    if not any(commit.parents):
        return [entry.fetch_blob() for entry in tree.values()]
    parent = commit
    for commit in repo.walk(commit.id, pygit2.GIT_SORT_TIME):
        if not any(unfinished):
            break
        right_tree = { entry.id.hex: AnnotatedTreeEntry(repo, entry)
                for entry in parent.tree }
        right_tree = set(v for v in right_tree.values())
        diff = left_tree - right_tree
        for entry in diff:
            if entry.id in tree:
                tree[entry.id].commit = commit
        unfinished = unfinished - diff
        left_tree = right_tree
        parent = commit

    cache = {entry.name: entry.serialize() for entry in tree.values()}
    cache = json.dumps(cache)
    redis.setex(key, timedelta(days=30), cache)

    return [entry.fetch_blob() for entry in tree.values()]

def _diffstat_name(delta, anchor):
    if delta.status == pygit2.GIT_DELTA_DELETED:
        return Markup(escape(delta.old_file.path))