1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package web
4
5import (
6 "context"
7 "io"
8
9 "bnema.dev/ermite/internal/domain"
10 "bnema.dev/ermite/internal/ports"
11)
12
13// The reads of the pages go through BrowseService as the caller of the
14// request. The revisions and paths the URLs carry are text: they are parsed
15// here, and one the domain refuses is an invalid argument, which the pages
16// answer like a missing one (fail).
17
18// commitList reads a page of the history that starts at rev, limited to path
19// when it is not empty.
20func (s *Server) commitList(ctx context.Context, repo domain.RepoName, rev, path string, size int, token string) (ports.HistoryPage, error) {
21 r, err := domain.ParseRevision(rev)
22 if err != nil {
23 return ports.HistoryPage{}, err
24 }
25 p, err := domain.ParseTreePath(path)
26 if err != nil {
27 return ports.HistoryPage{}, err
28 }
29 return s.cfg.Browse.ListCommits(ctx, principal(ctx), repo, ports.ListCommitsParams{
30 Revision: r, Path: p, PageSize: size, PageToken: token,
31 })
32}
33
34// getTree lists the directory path of the commit rev.
35func (s *Server) getTree(ctx context.Context, repo domain.RepoName, rev, path string, lastCommit bool) (domain.Tree, error) {
36 r, err := domain.ParseRevision(rev)
37 if err != nil {
38 return domain.Tree{}, err
39 }
40 p, err := domain.ParseTreePath(path)
41 if err != nil {
42 return domain.Tree{}, err
43 }
44 return s.cfg.Browse.GetTree(ctx, principal(ctx), repo, r, p, lastCommit)
45}
46
47// getCommit reads a commit with its diff.
48func (s *Server) getCommit(ctx context.Context, repo domain.RepoName, rev string, opts ports.GetCommitOptions) (domain.CommitDetail, error) {
49 r, err := domain.ParseRevision(rev)
50 if err != nil {
51 return domain.CommitDetail{}, err
52 }
53 return s.cfg.Browse.GetCommit(ctx, principal(ctx), repo, r, opts)
54}
55
56// openBlobAt opens the file path of the commit rev. The caller closes the
57// body.
58func (s *Server) openBlobAt(ctx context.Context, repo domain.RepoName, rev, path string) (domain.BlobInfo, io.ReadCloser, error) {
59 r, err := domain.ParseRevision(rev)
60 if err != nil {
61 return domain.BlobInfo{}, nil, err
62 }
63 if path == "" {
64 return domain.BlobInfo{}, nil, errNotFound
65 }
66 p, err := domain.ParseTreePath(path)
67 if err != nil {
68 return domain.BlobInfo{}, nil, err
69 }
70 return s.cfg.Browse.OpenBlob(ctx, principal(ctx), repo, r, p)
71}
72
73// resolveRevision resolves rev to the commit it designates.
74func (s *Server) resolveRevision(ctx context.Context, repo domain.RepoName, rev string) (domain.ResolvedRevision, error) {
75 r, err := domain.ParseRevision(rev)
76 if err != nil {
77 return domain.ResolvedRevision{}, err
78 }
79 return s.cfg.Browse.ResolveRevision(ctx, principal(ctx), repo, r)
80}
81
82// listPaths lists the file paths of the commit rev.
83func (s *Server) listPaths(ctx context.Context, repo domain.RepoName, rev string) (domain.PathList, error) {
84 r, err := domain.ParseRevision(rev)
85 if err != nil {
86 return domain.PathList{}, err
87 }
88 return s.cfg.Browse.ListPaths(ctx, principal(ctx), repo, r)
89}