repo.go

 1package file
 2
 3import (
 4	"os"
 5	"path/filepath"
 6	"strings"
 7
 8	"github.com/charmbracelet/soft-serve/git"
 9	"github.com/charmbracelet/soft-serve/server/backend"
10)
11
12var _ backend.Repository = (*Repo)(nil)
13
14// Repo is a filesystem Git repository.
15//
16// It implemenets backend.Repository.
17type Repo struct {
18	root string
19	path string
20}
21
22// Name returns the repository's name.
23//
24// It implements backend.Repository.
25func (r *Repo) Name() string {
26	name := strings.TrimSuffix(strings.TrimPrefix(r.path, r.root), ".git")
27	return strings.TrimPrefix(name, "/")
28}
29
30// ProjectName returns the repository's project name.
31func (r *Repo) ProjectName() string {
32	pn, err := readOneLine(filepath.Join(r.path, projectName))
33	if err != nil {
34		return ""
35	}
36
37	return strings.TrimSpace(pn)
38}
39
40// Description returns the repository's description.
41//
42// It implements backend.Repository.
43func (r *Repo) Description() string {
44	desc, err := readAll(filepath.Join(r.path, description))
45	if err != nil {
46		return ""
47	}
48
49	return strings.TrimSpace(desc)
50}
51
52// IsPrivate returns whether the repository is private.
53//
54// It implements backend.Repository.
55func (r *Repo) IsPrivate() bool {
56	_, err := os.Stat(filepath.Join(r.path, private))
57	return err == nil
58}
59
60// IsMirror returns whether the repository is a mirror.
61//
62// It implements backend.Repository.
63func (r *Repo) IsMirror() bool {
64	_, err := os.Stat(filepath.Join(r.path, mirror))
65	return err == nil
66}
67
68// Open returns the underlying git.Repository.
69//
70// It implements backend.Repository.
71func (r *Repo) Open() (*git.Repository, error) {
72	return git.Open(r.path)
73}