all repos — elgit @ a943f744c1f12ceda9e066cfe350c1aa782f3290

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

store repos in entries indexed by slug

Alan Pearce
commit

a943f744c1f12ceda9e066cfe350c1aa782f3290

parent

d8232be179233be6144ee12e4b537d0dd9eb68e8

4 files changed, 28 insertions(+), 20 deletions(-)

changed files
M data/entries.godata/entries.go
@@ -21,37 +21,48 @@ Repositories []*Repository
} type Entries struct { - Children []*Entry - Map map[string]*Entry + 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) { +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}, + Repositories: []*Repository{r}, }) return } - t, ok := ent.Map[r.Category] + cat, ok := ent.Categories[r.Category] if !ok { t := &Entry{ Name: r.Category, LastCommit: r.LastCommit, - Repositories: []*Repository{&r}, + Repositories: []*Repository{r}, } - ent.Map[r.Category] = t + ent.Categories[r.Category] = t return } - if t.LastCommit.IsZero() || t.LastCommit.Before(r.LastCommit) { - t.LastCommit = r.LastCommit + if cat.LastCommit.IsZero() || cat.LastCommit.Before(r.LastCommit) { + cat.LastCommit = r.LastCommit } - t.Repositories = append(t.Repositories, &r) + cat.Repositories = append(cat.Repositories, r) } func (ent *Entries) Sort() {
@@ -59,7 +70,7 @@ sort.Slice(ent.Children, func(i, j int) bool {
return ent.Children[i].LastCommit.After(ent.Children[j].LastCommit) }) - for _, entries := range ent.Map { + for _, entries := range ent.Categories { sort.Slice(entries.Repositories, func(i, j int) bool { return entries.Repositories[i].LastCommit.After(entries.Repositories[j].LastCommit) })
M routes/handler.goroutes/handler.go
@@ -113,7 +113,7 @@
return srv } -func (d deps) mountRepoPaths(router *atreugo.Router) { +func (d *deps) mountRepoPaths(router *atreugo.Router) { router.UseBefore(func(rc *atreugo.RequestCtx) error { category, _ := rc.UserValue("category").(string) name, _ := rc.UserValue("name").(string)
M routes/util.goroutes/util.go
@@ -41,7 +41,7 @@ return slices.Contains(d.c.Repo.Unlisted, name)
} func (d *deps) isNotAllowed(name string) bool { - return !slices.Contains(d.projects, name) + return d.repos.BySlug[name] == nil } func (d *deps) GetCleanPath(name string) (string, error) {
@@ -72,10 +72,7 @@ return projects, nil
} func (d *deps) UpdateRepos() { - entries := &data.Entries{ - Children: []*data.Entry{}, - Map: map[string]*data.Entry{}, - } + entries := data.NewEntries() for _, project := range d.projects { if project == "" || d.isUnlisted(project) {
@@ -104,14 +101,14 @@ }
if cc, err := repo.LastCommit(); err == nil { r.LastCommit = cc.Author.When } - entries.Add(r) + entries.Add(&r) } } } entries.Sort() - d.repos = entries + d.repos = &entries } func setContentDisposition(rc *atreugo.RequestCtx, name string) {
M templates/index.gotemplates/index.go
@@ -23,7 +23,7 @@ return g.If(len(entry.Repositories) == 1,
Repository(entry.Repositories[0], 0), ) }), - g.MapMap(entries.Map, func(category string, entry *data.Entry) g.Node { + g.MapMap(entries.Categories, func(category string, entry *data.Entry) g.Node { return g.Group{ Div(Class("index-category"), Header(g.Text(category)),