Example #1
0
        def __init__(self, path, patch, status):
            self.path = path
            self.patch = utils.highlight_code("name.diff", patch)
            self.status = status
            self.deleted = status == "D"

            # The line stats are not very elegant but difflib is kind of limited
            insert = len(re.findall(r"^\+", patch, re.MULTILINE)) - 1
            delete = len(re.findall(r"^-", patch, re.MULTILINE)) - 1
            self.insertion = f"++{insert}"
            self.deletion = f"--{delete}"
Example #2
0
def view(request, permission, repo_name, oid, path):
    if permission == permission.NO_ACCESS:
        raise Http404("no matching repository")

    db_repo_obj = get_object_or_404(Repository, name=repo_name)
    repo = mpygit.Repository(db_repo_obj.path)

    # First we normalize the path so libgit2 doesn't choke
    path = utils.normalize_path(path)

    commit = repo[oid]
    if commit is None or not isinstance(commit, mpygit.Commit):
        return HttpResponse("Invalid commit ID")

    # Resolve path inside commit
    obj = utils.resolve_path(repo, commit.tree, path)
    if obj == None:
        return HttpResponse("Invalid path")

    context = {
        "repo_name": repo_name,
        "oid": oid,
        "path": path,
        "branches": gen_branches(repo_name, repo, oid),
        "crumbs": gen_crumbs(repo_name, oid, path),
        "can_manage": permission == Permission.CAN_MANAGE,
    }

    if isinstance(obj, mpygit.Tree):
        template = "tree.html"
        context["entries"] = utils.tree_entries(repo, commit, path, obj)
    elif isinstance(obj, mpygit.Blob):
        template, code = read_blob(obj)
        if template == "blob.html":
            context["code"] = utils.highlight_code(path, code)
        else:
            context["code"] = code
        commit = gitutil.get_latest_change(repo, commit.oid,
                                           utils.split_path(path))
        context["change"] = commit
    else:
        return HttpResponse("Unsupported object type")

    return render(request, template, context=context)
Example #3
0
def view(request, permission, repo_name, oid, path):
    """Display blob or tree.

    If the path in the URL references a blob then the blob view template
    is rendered (which is specialised to whether the blob is a binary blob
    or not). Otherwise, a tree view is presented which displays the contents
    of a (sub)tree.

    Args:
        permission: permission rights of accessing user.
        repo_name: name of repository to inspect.
        path: path to blob or tree.
    """
    if permission == permission.NO_ACCESS:
        # TODO use Http404
        return HttpResponseNotFound("no matching repository")

    db_repo_obj = get_object_or_404(Repository, name=repo_name)
    repo = mpygit.Repository(db_repo_obj.path)

    # First we normalize the path so libgit2 doesn"t choke
    path = utils.normalize_path(path)

    try:
        commit = repo[oid]
    except KeyError:
        # TODO use Http404
        return HttpResponseNotFound("invalid head")

    if commit is None or not isinstance(commit, mpygit.Commit):
        return HttpResponse("Invalid commit ID")

    # Resolve path inside commit
    obj = utils.resolve_path(repo, commit.tree, path)
    if obj == None:
        return HttpResponse("Invalid path")

    context = {
        "repo_name": repo_name,
        "oid": oid,
        "path": path,
        "branches": gen_branches(repo_name, repo, oid),
        "crumbs": gen_crumbs(repo_name, oid, path),
        "can_manage": permission == Permission.CAN_MANAGE,
    }

    # specialise view to display object type correctly
    if isinstance(obj, mpygit.Tree):
        template = "tree.html"
        context["entries"] = utils.tree_entries(repo, commit, path, obj)
    elif isinstance(obj, mpygit.Blob):
        template, code = read_blob(obj)
        if template == "blob.html":
            # highlight code in textual blobs
            context["code"] = utils.highlight_code(path, code)
        else:
            context["code"] = code
        commit = gitutil.get_latest_change(repo, commit.oid,
                                           utils.split_path(path))
        context["change"] = commit
    else:
        return HttpResponse("Unsupported object type")

    return render(request, template, context=context)