2024-01-15 22:26:51 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2024-07-16 17:33:41 +00:00
|
|
|
"slices"
|
2024-01-15 22:26:51 +00:00
|
|
|
"sort"
|
2024-02-22 19:14:05 +00:00
|
|
|
"strings"
|
2024-01-15 22:26:51 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.jolheiser.com/ugit/internal/git"
|
|
|
|
"go.jolheiser.com/ugit/internal/html"
|
|
|
|
"go.jolheiser.com/ugit/internal/http/httperr"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (rh repoHandler) index(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
repoPaths, err := os.ReadDir(rh.s.RepoDir)
|
|
|
|
if err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
2024-07-16 17:33:41 +00:00
|
|
|
tagFilter := r.URL.Query().Get("tag")
|
|
|
|
|
2024-01-15 22:26:51 +00:00
|
|
|
repos := make([]*git.Repo, 0, len(repoPaths))
|
|
|
|
for _, repoName := range repoPaths {
|
2024-02-22 19:14:05 +00:00
|
|
|
if !strings.HasSuffix(repoName.Name(), ".git") {
|
|
|
|
continue
|
|
|
|
}
|
2024-01-15 22:26:51 +00:00
|
|
|
repo, err := git.NewRepo(rh.s.RepoDir, repoName.Name())
|
|
|
|
if err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
2024-07-26 20:01:38 +00:00
|
|
|
if repo.Meta.Private {
|
|
|
|
if !rh.s.ShowPrivate {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
repo.Meta.Tags = append(repo.Meta.Tags, "private")
|
2024-07-16 17:33:41 +00:00
|
|
|
}
|
2024-07-26 20:01:38 +00:00
|
|
|
|
2024-07-16 17:33:41 +00:00
|
|
|
if tagFilter != "" && !slices.Contains(repo.Meta.Tags, strings.ToLower(tagFilter)) {
|
|
|
|
continue
|
2024-01-15 22:26:51 +00:00
|
|
|
}
|
2024-07-16 17:33:41 +00:00
|
|
|
repos = append(repos, repo)
|
2024-01-15 22:26:51 +00:00
|
|
|
}
|
|
|
|
sort.Slice(repos, func(i, j int) bool {
|
|
|
|
var when1, when2 time.Time
|
|
|
|
if c, err := repos[i].LastCommit(); err == nil {
|
2024-01-16 04:54:43 +00:00
|
|
|
when1 = c.When
|
2024-01-15 22:26:51 +00:00
|
|
|
}
|
|
|
|
if c, err := repos[j].LastCommit(); err == nil {
|
2024-01-16 04:54:43 +00:00
|
|
|
when2 = c.When
|
2024-01-15 22:26:51 +00:00
|
|
|
}
|
|
|
|
return when1.After(when2)
|
|
|
|
})
|
|
|
|
|
|
|
|
links := make([]html.IndexLink, 0, len(rh.s.Profile.Links))
|
|
|
|
for _, link := range rh.s.Profile.Links {
|
|
|
|
links = append(links, html.IndexLink{
|
|
|
|
Name: link.Name,
|
|
|
|
URL: link.URL,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := html.Index(html.IndexContext{
|
|
|
|
BaseContext: rh.baseContext(),
|
|
|
|
Profile: html.IndexProfile{
|
|
|
|
Username: rh.s.Profile.Username,
|
|
|
|
Email: rh.s.Profile.Email,
|
|
|
|
Links: links,
|
|
|
|
},
|
2024-01-20 02:34:27 +00:00
|
|
|
Repos: repos,
|
2024-01-15 22:26:51 +00:00
|
|
|
}).Render(r.Context(), w); err != nil {
|
|
|
|
return httperr.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|