1package proto
 2
 3import (
 4	"path/filepath"
 5
 6	"github.com/charmbracelet/soft-serve/git"
 7	"github.com/gobwas/glob"
 8)
 9
10// Metadata is a repository's metadata.
11type Metadata interface {
12	Name() string
13	ProjectName() string
14	Description() string
15	IsPrivate() bool
16	Collabs() []User
17	Open() (Repository, error)
18}
19
20// Repository is Git repository.
21type Repository interface {
22	Name() string
23	Repository() *git.Repository
24}
25
26// LatestFile returns the contents of the latest file at the specified path in
27// the repository and its file path.
28func LatestFile(r Repository, pattern string) (string, string, error) {
29	g := glob.MustCompile(pattern)
30	dir := filepath.Dir(pattern)
31	head, err := r.Repository().HEAD()
32	if err != nil {
33		return "", "", err
34	}
35	t, err := r.Repository().TreePath(head, dir)
36	if err != nil {
37		return "", "", err
38	}
39	ents, err := t.Entries()
40	if err != nil {
41		return "", "", err
42	}
43	for _, e := range ents {
44		fp := filepath.Join(dir, e.Name())
45		if e.IsTree() {
46			continue
47		}
48		if g.Match(fp) {
49			bts, err := e.Contents()
50			if err != nil {
51				return "", "", err
52			}
53			return string(bts), fp, nil
54		}
55	}
56	return "", "", git.ErrFileNotFound
57}