repo.go

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