← history for internal/routes/repoindex.go
f861c317internal/routes/repoindex.go106 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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
/**
 * 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 routes

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

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

// HandleIndex GET /:repo/  — file list
func HandleIndex(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, "files",
			template.HTML(fmt.Sprintf(
				`<a href="/">repos</a> <span class="sep">/</span> <a href="/%s/">%s</a>`,
				url.PathEscape(repoName), template.HTMLEscapeString(repoName),
			)))
		files, cmd, err := gitrepo.ListFiles(rp, pd.Branch)

		readmeHTML := rendering.RenderReadme(rp, pd.Branch)

		var body strings.Builder
		body.WriteString(fmt.Sprintf(
			`<h1 class="page-title"><em>%s</em></h1>`+
				`<p class="page-sub">branch: <strong>%s</strong> &nbsp;·&nbsp; %d files</p>`,
			template.HTMLEscapeString(repoName),
			template.HTMLEscapeString(pd.Branch), len(files),
		))
		body.WriteString(fmt.Sprintf(
			`<div style="display:flex;align-items:center;gap:8px;padding:5px 7px;
    background:var(--code-bg,#f5f5f5);border-radius:8px;
    border:1px solid #ddd;font-family:monospace;font-size:12px;max-width:600px">
        <span style="color:#6a737d;user-select:none">$</span>
        <span id="clone-cmd" style="flex:1;color:#24292e">git clone %s</span>
        <button onclick="
        navigator.clipboard.writeText('git clone %s');
        this.textContent='Copied!';
        setTimeout(()=>this.textContent='Copy',1500)
        " style="padding:4px 10px;font-size:12px;border:1px solid #ccc;
        border-radius:5px;background:#fff;cursor:pointer">Copy</button>
        </div>
        <br/>`,
			resolveCloneUrl(cfg, c, repoName),
			resolveCloneUrl(cfg, c, repoName),
		))

		// ── Inject README before file table ─────────────────────────────────────
		if readmeHTML != "" {
			body.WriteString(`<div class="readme-box">`)
			body.WriteString(`<div class="readme-header">`)
			body.WriteString(`<span class="file-icon">📄</span> README.md`)
			body.WriteString(`</div>`)
			body.WriteString(`<div class="readme-body">`)
			body.WriteString(readmeHTML)
			body.WriteString(`</div></div>`)
		}
		// ────────────────────────────────────────────────────────────────────────

		if err != nil || len(files) == 0 {
			body.WriteString(`<div class="empty">No tracked files found on this branch.</div>`)
		} else {
			body.WriteString(`<table class="data-table"><thead><tr><th>File</th><th>Commits</th></tr></thead><tbody>`)
			for _, f := range files {
				parts := strings.Split(f.Path, "/")
				name := parts[len(parts)-1]
				dir := ""
				if len(parts) > 1 {
					dir = strings.Join(parts[:len(parts)-1], "/") + "/"
				}
				body.WriteString(fmt.Sprintf(
					`<tr><td><span class="file-icon">%s</span>`+
						`<span class="muted-text">%s</span>`+
						`<a class="file-link" href="/%s/history?path=%s&branch=%s">%s</a></td>`+
						`<td class="muted-text">%d</td></tr>`,
					rendering.FileIcon(name), template.HTMLEscapeString(dir),
					url.PathEscape(repoName), url.QueryEscape(f.Path), url.QueryEscape(pd.Branch),
					template.HTMLEscapeString(name), f.CommitCount,
				))
			}
			body.WriteString("</tbody></table>")
		}
		pd.Title = repoName + " — files"
		pd.GitCmd = cmd
		pd.Body = template.HTML(body.String())
		rendering.RenderPage(c, pd)
	}
}