<commit_before>from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.views.generic import (RedirectView, ListView, DetailView,
                                  TemplateView)
from django.views.generic.detail import SingleObjectMixin
from django.views.generic.base import View
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from django.http import Http404, HttpResponse
from django.conf import settings

from guardian.mixins import PermissionRequiredMixin

from dulwich.objects import Tag, Commit

from .models import Repository
from .utils import is_binary, image_mimetype

import re
import logging

logger = logging.getLogger(__name__)


class GretaMixin(PermissionRequiredMixin, SingleObjectMixin):
    model = Repository
    context_object_name = "repo"
    permission_required = "greta.can_view_repository"
    raise_exception = True
    sha_re = re.compile(r'^[a-f0-9]{40}$')

    def dispatch(self, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs
        self.request = args[0]
        return super(GretaMixin, self).dispatch(*args, **kwargs)

    def get(self, *args, **kwargs):
        # Check if the repo is ready
        if not self.get_object().is_ready():
            context = RequestContext(self.request, {})
            return render_to_response("greta/repository_not_ready.html",
                                      context)
        self.full_ref = self.validate_ref(self.kwargs['ref'])
        return super(GretaMixin, self).get(*args, **kwargs)

    def validate_ref(self, ref):
        sha_match = self.sha_re.match(ref)
        if sha_match is not None:
            # If it's a valid sha1 hash, we're good.
            return sha_match.group(0)

        repo = self.get_object()
        if not ref.startswith('refs'):
            # If it doesn't start with 'refs/heads, we'll assume it's
            # a branch name.
            ref = 'refs/heads/' + ref

        # If it's in the list of refs, we're solid.
        if ref in repo.repo.get_refs():
            return ref

        # If the repo's empty, let them through
        if len(repo.repo.get_refs()) == 0:
            return "empty"

        # If we haven't returned yet, the ref must be no good.
        logger.debug('Invalid ref: "%s"' % ref)
        raise Http404("No such ref.")

    def get_context_data(self, **kwargs):
        context = super(GretaMixin, self).get_context_data(**kwargs)

        # Add ref to contect
        context['ref'] = self.kwargs['ref']
        context['full_ref'] = self.full_ref<commit_msg>Update views to better validate refs<commit_after>from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.views.generic import (RedirectView, ListView, DetailView,
                                  TemplateView)
from django.views.generic.detail import SingleObjectMixin
from django.views.generic.base import View
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from django.http import Http404, HttpResponse
from django.conf import settings

from guardian.mixins import PermissionRequiredMixin

from dulwich.objects import Tag, Commit

from .models import Repository
from .utils import is_binary, image_mimetype

import re
import logging

logger = logging.getLogger(__name__)


class GretaMixin(PermissionRequiredMixin, SingleObjectMixin):
    model = Repository
    context_object_name = "repo"
    permission_required = "greta.can_view_repository"
    raise_exception = True
    sha_re = re.compile(r'^[a-f0-9]{40}$')

    def dispatch(self, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs
        self.request = args[0]
        return super(GretaMixin, self).dispatch(*args, **kwargs)

    def get(self, *args, **kwargs):
        # Check if the repo is ready
        if not self.get_object().is_ready():
            context = RequestContext(self.request, {})
            return render_to_response("greta/repository_not_ready.html",
                                      context)
        self.full_ref = self.validate_ref(self.kwargs['ref'])
        return super(GretaMixin, self).get(*args, **kwargs)

    def validate_ref(self, ref):
        repo = self.get_object()

        if ref == "HEAD":
            ref = repo.repo.head()

        sha_match = self.sha_re.match(ref)
        if sha_match is not None:
            # If it's a valid sha1 hash, we're good.
            sha = sha_match.group(0)
            for ref, ref_sha in repo.repo.refs.as_dict().iteritems():
                if sha == ref_sha:
                    return ref
            return sha

        if not ref.startswith('refs'):
            # If it doesn't start with 'refs/heads, we'll assume it's
            # a branch name.
            ref = 'refs/heads/' + ref

        try:
            # If the ref maps to a sha without throwing an exception,
            # it's valid
            repo.repo.refs[ref]
            return ref
        except KeyError:
            pass

        # If the repo's empty, let them through
        if len(repo.repo.get_refs()) == 0:
            return "empty"

        # If we haven't returned yet, the ref must be no good.
        logger.debug('Invalid ref: "%s"' % ref)
        raise Http404("No such ref.")

    def get_context_data(self, **kwargs):
        context = super(GretaMixin, self).get_context_data(**kwargs)

        # Add ref to contect
        context['ref'] = self.kwargs['ref']
        context['full_ref'] = self.full_ref
{'fraction_non_alphanumeric': 0.09648493543758967, 'fraction_numerical': 0.006097560975609756, 'mean_word_length': 2.4899874843554444, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '764054', 'n_tokens_mistral': 1645, 'n_tokens_neox': 1560, 'n_words': 454}