← history for internal/handler/handle_history.go
4e7342aeinternal/handler/handle_history.go84 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
79
80
81
82
83
84
/**
 * 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/http"
    "net/url"
    "strings"

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

// HandleHistory GET /:repo/history?path=<f>&branch=<b>
func HandleHistory(cfg config.AltGitConfiguration) gin.HandlerFunc {
    return func(c *gin.Context) {
        repoName, rp, ok := rendering.ResolveRepo(cfg, c.Param("repo"))
        if !ok {
            return
        }
        path := c.Query("path")
        if path == "" {
            c.Redirect(http.StatusFound, "/"+repoName+"/")
            return
        }
        pd := rendering.BasePageData(c, repoName, rp, "files",
            template.HTML(fmt.Sprintf(
                `<a href="/">repos</a> <span class="sep">/</span> <a href="/%s/">%s</a> <span class="sep">/</span> %s`,
                url.PathEscape(repoName), template.HTMLEscapeString(repoName),
                template.HTMLEscapeString(path),
            )))

        commits, cmd, err := gitrepo.FileHistory(rp, path, pd.Branch)

        var body strings.Builder
        body.WriteString(fmt.Sprintf(`<a class="back-link" href="/%s/?branch=%s">← all files</a>`,
            url.PathEscape(repoName), url.QueryEscape(pd.Branch)))
        body.WriteString(fmt.Sprintf(
            `<h1 class="page-title"><em>%s</em></h1>`+
                `<p class="page-sub">Version history &nbsp;·&nbsp; %d commits</p>`,
            template.HTMLEscapeString(path), len(commits),
        ))

        if err != nil || len(commits) == 0 {
            body.WriteString(`<div class="empty">No history found for this file.</div>`)
        } else {
            body.WriteString(`<div class="version-list">`)
            for _, cm := range commits {
                body.WriteString(fmt.Sprintf(
                    `<div class="version-item">`+
                        `<a class="log-hash" href="/%s/view?path=%s&hash=%s&branch=%s">%s</a>`+
                        `<span class="log-msg">%s</span>`+
                        `<span class="log-author">%s</span>`+
                        `<span class="log-date">%s</span>`+
                        `</div>`,
                    url.PathEscape(repoName),
                    url.QueryEscape(path), url.QueryEscape(cm.Hash), url.QueryEscape(pd.Branch),
                    template.HTMLEscapeString(cm.Short),
                    template.HTMLEscapeString(cm.Message),
                    template.HTMLEscapeString(cm.Author),
                    template.HTMLEscapeString(cm.Date),
                ))
            }
            body.WriteString("</div>")
        }

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