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// Description returns the repository's description.
31//
32// It implements backend.Repository.
33func (r *Repo) Description() string {
34	desc, err := readAll(filepath.Join(r.path, description))
35	if err != nil {
36		logger.Debug("failed to read description file", "err", err,
37			"path", filepath.Join(r.path, description))
38		return ""
39	}
40
41	return strings.TrimSpace(desc)
42}
43
44// IsPrivate returns whether the repository is private.
45//
46// It implements backend.Repository.
47func (r *Repo) IsPrivate() bool {
48	_, err := os.Stat(filepath.Join(r.path, private))
49	return err == nil
50}
51
52// Open returns the underlying git.Repository.
53//
54// It implements backend.Repository.
55func (r *Repo) Open() (*git.Repository, error) {
56	return git.Open(r.path)
57}