all repos — elgit @ 4824077ca3d2cd326880229ba3985bcf70a1fafa

fork of legit: web frontend for git, written in go

data/entries.go (view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package data

import (
	"sort"
	"time"

	"alin.ovh/elgit/git"
)

type Repository struct {
	Name          string
	Category      string
	Path          string
	Slug          string
	Description   string
	LastCommit    time.Time
	LastCommits   []*git.CommitReference
	ReadmeContent string
	MainBranch    string
	Gomod         bool
	*git.Repo
}

type Entry struct {
	Name         string
	LastCommit   time.Time
	Repositories []*Repository
}

type Entries struct {
	BySlug     map[string]*Repository
	Children   []*Entry
	Categories map[string]*Entry
}

func NewEntries() Entries {
	return Entries{
		BySlug:     map[string]*Repository{},
		Children:   []*Entry{},
		Categories: map[string]*Entry{},
	}
}

func (ent *Entries) Add(r *Repository) {
	ent.BySlug[r.Slug] = r

	if r.Category == "" {
		ent.Children = append(ent.Children, &Entry{
			Name:         r.Name,
			LastCommit:   r.LastCommit,
			Repositories: []*Repository{r},
		})

		return
	}
	cat, ok := ent.Categories[r.Category]
	if !ok {
		t := &Entry{
			Name:         r.Category,
			LastCommit:   r.LastCommit,
			Repositories: []*Repository{r},
		}
		ent.Categories[r.Category] = t

		return
	}

	if cat.LastCommit.IsZero() || cat.LastCommit.Before(r.LastCommit) {
		cat.LastCommit = r.LastCommit
	}

	cat.Repositories = append(cat.Repositories, r)
}

func (ent *Entries) Sort() {
	sort.Slice(ent.Children, func(i, j int) bool {
		return ent.Children[i].LastCommit.After(ent.Children[j].LastCommit)
	})

	for _, entries := range ent.Categories {
		sort.Slice(entries.Repositories, func(i, j int) bool {
			return entries.Repositories[i].LastCommit.After(entries.Repositories[j].LastCommit)
		})
	}
}