← history for repobrowser/internal/utils/utils.go
a87fd589repobrowser/internal/utils/utils.go73 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
package utils

import (
    "html/template"
    "net/http"
    "os"
    "path/filepath"
    "repobrowser/internal/config"
    "repobrowser/internal/git"
    "repobrowser/internal/models"
    "repobrowser/internal/templating"
    "strings"

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

func RenderPage(c *gin.Context, data models.PageData) {
    tmpl, err := template.New("base").Parse(templating.TemplateBase)
    if err != nil {
        c.String(http.StatusInternalServerError, err.Error())
        return
    }
    c.Status(http.StatusOK)
    c.Header("Content-Type", "text/html; charset=utf-8")
    tmpl.Execute(c.Writer, data)
}

func ActiveBranch(c *gin.Context, def string) string {
    if b := c.Query("branch"); b != "" {
        return b
    }
    return def
}

func RepoPath(cfg config.RepositoryBrowserConfiguration, name string) string {
    if name == "" || strings.Contains(name, "/") || strings.Contains(name, "..") {
        return ""
    }
    p := filepath.Join(cfg.Root, name)
    // Confirm it stays inside reposRoot
    rel, err := filepath.Rel(cfg.Root, p)
    if err != nil || strings.HasPrefix(rel, "..") {
        return ""
    }
    return p
}

// resolveRepo validates the :repo param and returns (repoName, repoPath).
// Aborts with 404 on bad input.
func ResolveRepo(cfg config.RepositoryBrowserConfiguration, name string) (string, string, bool) {
    //name := c.Param("repo")
    p := RepoPath(cfg, name)
    if p == "" {
        //c.String(http.StatusNotFound, "repository not found")
        return "", "", false
    }
    if _, err := os.Stat(p); err != nil {
        //c.String(http.StatusNotFound, "repository not found")
        return "", "", false
    }
    return name, p, true
}
func BasePageData(c *gin.Context, repoName, rp, tab string, crumb template.HTML) models.PageData {
    all, cur := git.ListBranches(rp)
    branch := ActiveBranch(c, cur)
    return models.PageData{
        Repo:       repoName,
        Branch:     branch,
        Branches:   git.BuildBranchLinks(all, branch, repoName, tab),
        ActiveTab:  tab,
        Breadcrumb: crumb,
    }
}