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