ikurotime / gitgud

public
main / internal/app/browse_service.go
2.2 KB · Go Raw
 1package app
 2
 3import (
 4	"context"
 5
 6	"gitgud/internal/domain"
 7)
 8
 9type BrowseService struct {
10	repos domain.RepositoryRepository
11	git   domain.GitReader
12}
13
14func NewBrowseService(repos domain.RepositoryRepository, git domain.GitReader) *BrowseService {
15	return &BrowseService{repos: repos, git: git}
16}
17
18// Repo loads the repository and enforces the viewer's permission. The returned
19// repo is safe to pass to the other methods, which assume it is authorized.
20func (s *BrowseService) Repo(ctx context.Context, owner, name string, viewer *domain.User) (*domain.Repository, error) {
21	repo, err := s.repos.ByOwnerAndName(ctx, owner, name)
22	if err != nil {
23		return nil, err
24	}
25	if err := CanView(repo, viewer); err != nil {
26		return nil, err
27	}
28	return repo, nil
29}
30
31func (s *BrowseService) ref(repo *domain.Repository, ref string) string {
32	if ref == "" {
33		return repo.DefaultBranch
34	}
35	return ref
36}
37
38func (s *BrowseService) IsEmpty(ctx context.Context, repo *domain.Repository) (bool, error) {
39	return s.git.IsEmpty(ctx, repo.OwnerName, repo.Name)
40}
41
42func (s *BrowseService) Branches(ctx context.Context, repo *domain.Repository) ([]string, error) {
43	return s.git.Branches(ctx, repo.OwnerName, repo.Name)
44}
45
46func (s *BrowseService) Tip(ctx context.Context, repo *domain.Repository, ref string) (*domain.Commit, error) {
47	return s.git.Tip(ctx, repo.OwnerName, repo.Name, s.ref(repo, ref))
48}
49
50func (s *BrowseService) Tree(ctx context.Context, repo *domain.Repository, ref, path string) ([]domain.TreeEntry, error) {
51	return s.git.Tree(ctx, repo.OwnerName, repo.Name, s.ref(repo, ref), path)
52}
53
54func (s *BrowseService) Blob(ctx context.Context, repo *domain.Repository, ref, path string) (*domain.FileBlob, error) {
55	return s.git.Blob(ctx, repo.OwnerName, repo.Name, s.ref(repo, ref), path)
56}
57
58func (s *BrowseService) Log(ctx context.Context, repo *domain.Repository, ref string, limit, offset int) ([]domain.Commit, error) {
59	return s.git.Log(ctx, repo.OwnerName, repo.Name, s.ref(repo, ref), limit, offset)
60}
61
62func (s *BrowseService) CommitDiff(ctx context.Context, repo *domain.Repository, hash string) (*domain.Commit, []domain.FileDiff, error) {
63	return s.git.CommitDiff(ctx, repo.OwnerName, repo.Name, hash)
64}