← history for internal/handler/handle_repo_list.go
4e7342aeinternal/handler/handle_repo_list.go78 lines⬡ raw↓ download
 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
/**
 * AltGit - altgit
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Paolo Lulli <kevwe.com>
 * @copyright Paolo Lulli 2026
 */

package handler

import (
    "altgit/internal/config"
    "altgit/internal/gitrepo"
    "altgit/internal/rendering"
    "fmt"
    "html/template"
    "net/url"
    "strings"

    "github.com/gin-gonic/gin"
)

// HandleRepoList GET /  — list all repos
func HandleRepoList(cfg config.AltGitConfiguration) gin.HandlerFunc {
    return func(c *gin.Context) {

        repos := gitrepo.ListRepos(cfg)

        var body strings.Builder
        body.WriteString(fmt.Sprintf(
            `<h1 class="page-title">Repositories</h1>`+
                `<p class="page-sub"> %d repositories</p>`,
            len(repos),
        ))

        if len(repos) == 0 {
            body.WriteString(`<div class="empty">No git repositories found.</div>`)
        } else {
            body.WriteString(`<div class="repo-grid">`)
            for _, r := range repos {
                meta := r.Branch
                if r.LastCommit != "" {
                    meta += " &nbsp;·&nbsp; " + template.HTMLEscapeString(r.LastCommit)
                }
                body.WriteString(fmt.Sprintf(
                    `<a class="repo-card" href="/%s/">`+
                        `<div class="repo-card-name">💽 %s</div>`+
                        `<div class="repo-card-desc">%s</div>`+
                        `<div class="repo-card-meta">%s</div>`+
                        `</a>`,
                    url.PathEscape(r.Name),
                    template.HTMLEscapeString(r.Name),
                    template.HTMLEscapeString(r.Description),
                    meta,
                ))
            }
            body.WriteString("</div>")
        }

        rendering.RenderPage(cfg, c, rendering.PageData{
            Title:      "altgit",
            Breadcrumb: template.HTML(`<span style="color:var(--muted)">repositories</span>`),
            //GitCmd:     fmt.Sprintf("ls %s", cfg.Root),
            GitCmd: fmt.Sprintf("ls <root-dir>"), // Do not show the root filepath for security reasons
            Body:   template.HTML(body.String()),
        })
    }
}

func resolveCloneUrl(cfg config.AltGitConfiguration, c *gin.Context, repoName string) string {
    if "" == cfg.Domain {
        return fmt.Sprintf("http://%s/git/%s", c.Request.Host, repoName)
    }
    return fmt.Sprintf("https://%s/git/%s", cfg.Domain, repoName)

}