← history for internal/handler/handle_refs.go
4e7342aeinternal/handler/handle_refs.go68 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
/**
 * 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"
)

// HandleRefs GET /:repo/refs
func HandleRefs(cfg config.AltGitConfiguration) gin.HandlerFunc {
    return func(c *gin.Context) {
        repoName, rp, ok := rendering.ResolveRepo(cfg, c.Param("repo"))
        if !ok {
            return
        }
        pd := rendering.BasePageData(c, repoName, rp, "refs",
            template.HTML(fmt.Sprintf(
                `<a href="/">repos</a> <span class="sep">/</span> <a href="/%s/">%s</a> <span class="sep">/</span> refs`,
                url.PathEscape(repoName), template.HTMLEscapeString(repoName),
            )))

        refs, cmd, err := gitrepo.ListRefs(rp)

        var body strings.Builder
        body.WriteString(`<h1 class="page-title">Refs</h1><p class="page-sub">Branches and tags in this repository</p>`)

        if err != nil || len(refs) == 0 {
            body.WriteString(`<div class="empty">No refs found.</div>`)
        } else {
            body.WriteString(`<table class="data-table ref-row"><thead><tr><th></th><th>Name</th><th>Type</th><th>Hash</th></tr></thead><tbody>`)
            for _, r := range refs {
                badgeClass, badgeLabel, icon := "ref-branch", "branch", "⎇"
                if r.Kind == "tag" {
                    badgeClass, badgeLabel, icon = "ref-tag", "tag", "🏷"
                }
                body.WriteString(fmt.Sprintf(
                    `<tr><td>%s</td><td><span class="ref-name">%s</span></td>`+
                        `<td><span class="ref-badge %s">%s</span></td>`+
                        `<td><span class="ref-hash">%s</span></td></tr>`,
                    icon, template.HTMLEscapeString(r.Name),
                    badgeClass, badgeLabel, template.HTMLEscapeString(r.Short),
                ))
            }
            body.WriteString("</tbody></table>")
        }

        pd.Title = repoName + " — refs"
        pd.GitCmd = cmd
        pd.Body = template.HTML(body.String())
        rendering.RenderPage(cfg, c, pd)
    }
}